Skip to content

Commit 724d1df

Browse files
author
Himanshu Upreti
committed
twilio otp verification
1 parent f939fd7 commit 724d1df

File tree

9 files changed

+249
-3
lines changed

9 files changed

+249
-3
lines changed

client/src/app/_services/auth.service.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,28 @@ export class AuthService {
5454
return user.token;
5555
}
5656

57+
58+
public generateOtp(){
59+
return this.http.post(
60+
`${this.apiBaseUrl}/users/otp`,{}
61+
);
62+
}
63+
64+
65+
66+
public verifyOtp(otp){
67+
const params = {
68+
otp: otp
69+
};
70+
return this.http.get(
71+
`${this.apiBaseUrl}/users/otp`,{
72+
params: params
73+
}
74+
);
75+
}
76+
77+
78+
5779
public getUserDetails() {
5880
return this.http.get(
5981
`${this.apiBaseUrl}/users/details`

client/src/app/app.component.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@
22
<div id="navbar">
33
<app-navbar></app-navbar>
44
</div>
5+
<div id="otp-banner" >
6+
<div class="columns is-centered has-text-centered">
7+
<div class="column is-2" *ngIf="!otpGenerated">
8+
<a class="button is-primary" (click)="generateOtp()">Click here to verify your Mobile Number</a>
9+
</div>
10+
<ng-container *ngIf="otpGenerated">
11+
<div class="column is-2">
12+
<div class="field">
13+
<div class="control">
14+
<input class="input" type="number" placeholder="OTP" [(ngModel)]="otp" name="otp" required>
15+
</div>
16+
</div>
17+
</div>
18+
<div class="column is-2">
19+
<a class="button is-primary" (click)="verifyOtp()">Verify OTP</a>
20+
</div>
21+
</ng-container>
22+
23+
</div>
24+
</div>
525
<div id="body">
626
<router-outlet></router-outlet>
727
</div>

client/src/app/app.component.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Component } from '@angular/core';
22
import { SEOService } from './_services/seo.service';
3+
import { AuthService } from './_services/auth.service';
34

45
@Component({
56
selector: 'app-root',
@@ -8,10 +9,43 @@ import { SEOService } from './_services/seo.service';
89
})
910
export class AppComponent {
1011
title = 'relief-shelter';
12+
user: any;
13+
otp: any;
14+
otpGenerated = false;
1115

1216
constructor(
13-
private seoService: SEOService
17+
private seoService: SEOService,
18+
private authService: AuthService
1419
) {
1520
this.seoService.addSeoData();
1621
}
22+
23+
generateOtp(){
24+
this.authService.generateOtp()
25+
.subscribe(
26+
(res) => {
27+
this.otpGenerated= true;
28+
29+
}
30+
)
31+
}
32+
verifyOtp(){
33+
this.authService.verifyOtp(this.otp)
34+
.subscribe(
35+
(res) => {
36+
this.user.verifiedMobile = true;
37+
}
38+
)
39+
40+
}
41+
ngOnInit(): void {
42+
//Called after the constructor, initializing input properties, and the first call to ngOnChanges.
43+
//Add 'implements OnInit' to the class.
44+
this.authService.getUserDetails()
45+
.subscribe(
46+
(res) => {
47+
this.user = res;
48+
}
49+
)
50+
}
1751
}

client/src/app/home/home.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class HomeComponent implements AfterViewInit {
4040
accessToken: mapboxgl.accessToken
4141
}));
4242

43-
this.sheltersService.getAllShelters()
43+
this.sheltersService.getAllShelters(72,72,50)
4444
.subscribe(
4545
(res) => {
4646
var i = 0;

client/src/environments/environment.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const environment = {
77
apiBaseUrl: 'http://localhost:3000/api',
88
mapboxAccessToken: 'pk.eyJ1Ijoicm9oaXRycCIsImEiOiJjam5vejdtYmYyMG1tM3FzNXA1YnVib3JiIn0.5x5lvYoSAxmmc3EQAg19og',
99
googleMapsAPIKey: 'AIzaSyDoaA5lB4-JbSXElDWllnNU3QcArgxdreM'
10+
1011
};
1112

1213
/*

package-lock.json

Lines changed: 65 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/api/users/model/User.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ const userModel = new Schema({
3131
firstName: { type: String },
3232
lastName: { type: String },
3333
mobileNumber: { type: Number },
34-
aadharNumber: { type: Number }
34+
aadharNumber: { type: Number },
35+
verifiedMobile: {type: Boolean, default: false }
3536

3637
});
3738
module.exports = mongoose.model('User', userModel);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
var rp = require('request-promise');
2+
const User = require('../model/User')
3+
const authyBaseUrl = process.env.TWILIO_API_URL
4+
const API_KEY = process.env.TWILIO_API_KEY
5+
6+
module.exports = {
7+
method: 'POST',
8+
path: '/api/users/otp',
9+
options: {
10+
handler: async (request, h) => {
11+
const user = await User.findOne({_id: request.auth.credentials.id})
12+
const options = {
13+
method:'POST',
14+
url: `${authyBaseUrl}/start`,
15+
form:{
16+
api_key: API_KEY,
17+
phone_number : user.mobileNumber,
18+
via: 'sms',
19+
country_code:91
20+
}
21+
22+
}
23+
let res = await rp(options)
24+
return {message: 'successful'}
25+
},
26+
auth: {
27+
strategy: 'jwt'
28+
},
29+
// validate: {
30+
// payload: addShelterSchema
31+
// },
32+
description: 'Add shelter',
33+
notes: 'Adds shelter',
34+
tags: ['api', 'shelter']
35+
}
36+
}
37+
38+
39+
// PhoneVerification.prototype.verifyPhoneToken = function (phone_number, country_code, token, callback) {
40+
41+
// console.log('in verify phone');
42+
// this._request("get", "/protected/json/phones/verification/check", {
43+
// "api_key": this.apiKey,
44+
// "verification_code": token,
45+
// "phone_number": phone_number,
46+
// "country_code": country_code
47+
// },
48+
// callback
49+
// );
50+
// };
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
var rp = require('request-promise');
2+
const User = require('../model/User')
3+
const authyBaseUrl = process.env.TWILIO_API_URL
4+
const API_KEY = process.env.TWILIO_API_KEY
5+
6+
module.exports = {
7+
method: 'GET',
8+
path: '/api/users/otp',
9+
options: {
10+
handler: async (request, h) => {
11+
const user = await User.findOne({_id: request.auth.credentials.id})
12+
const options = {
13+
method:'GET',
14+
url: `${authyBaseUrl}/check`,
15+
form:{
16+
api_key: API_KEY,
17+
phone_number : user.mobileNumber,
18+
via: 'sms',
19+
country_code:91,
20+
verification_code: request.query.otp
21+
}
22+
23+
}
24+
// console.log(options)
25+
let res = await rp(options)
26+
// console.log(res)
27+
return res
28+
},
29+
auth: {
30+
strategy: 'jwt'
31+
},
32+
// validate: {
33+
// payload: addShelterSchema
34+
// },
35+
description: 'Add shelter',
36+
notes: 'Adds shelter',
37+
tags: ['api', 'shelter']
38+
}
39+
}
40+
41+
42+
// PhoneVerification.prototype.verifyPhoneToken = function (phone_number, country_code, token, callback) {
43+
44+
// console.log('in verify phone');
45+
// this._request("get", "/protected/json/phones/verification/check", {
46+
// "api_key": this.apiKey,
47+
// "verification_code": token,
48+
// "phone_number": phone_number,
49+
// "country_code": country_code
50+
// },
51+
// callback
52+
// );
53+
// };

0 commit comments

Comments
 (0)