Skip to content

Commit

Permalink
socket room programming
Browse files Browse the repository at this point in the history
  • Loading branch information
ChauhanAbhinav committed Aug 14, 2019
1 parent 4b57df3 commit d34558a
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 66 deletions.
5 changes: 4 additions & 1 deletion server/db/services/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ let ifRegistered = (user)=>{

}
else
resolve(user);console.log();
{
// console.log('user found: ', user)
resolve(user);
}
}
});

Expand Down
89 changes: 46 additions & 43 deletions server/routes/loginRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,82 @@ const express = require('express');
const app = express();
const router = express.Router();
const twilio = require('../twilio/index');

let user= {};
//importing user services
let userService = require('../db/services/user.service');

router.post('/login',(req, res)=>{
// check if registered

console.log(req.body);
router.post('/login',(req, res)=>{
// console.log(req.body);
if(!req.body.otp){
userService.ifRegistered(req.body)
.then(function (user) {
// authentication successful

console.log('User is registered');
//send otp

// twilio.sendAuthyToken(user,(response, user)=>{
// // success callback
// console.log(response);
// res.status(200).json('User is registered, otp sent'+JSON.stringify(response));

// // twilio.verifyAuthyToken(user, user.otp, (error, response)=>{
// // // console.log('authy id',user.authyId) // 179825188
// // if(error){
// // console.log('Otp Varifucation error:',error)
// // }
// // else{
// // console.log('otp varification successful: ',response)
// // }
// // })

// },
// (err)=>{
// //error callback
// console.log('error: ',err);
// res.status(400).json('User is registered, otp is not sent, Error: '+JSON.stringify(err.message));
// });
res.status('200').json('');
twilio.verifyAuthyToken(179825188, user.otp, (error, response)=>{
if(error){
console.log('Otp Varifucation error:',error)
}
else{
console.log('otp varification successful: ',response)
}
});
res.redirect(307,'/sendtoken');
}, function(err) {
console.log(err);
// User is not registered
console.log(err);
res.status(404); //204: no content, 404: not found
res.redirect(307,'/register'); // temporary redirect with same data

})
} else{
res.redirect(307,'/varifytoken');
}

})
// register user

router.post('/register',(req, res)=>{

console.log(req.body);
userService.createUser(req.body)
.then(function (data) {
// registration successful
console.log('Registration successful');
res.status(201).json('Registration successful'); //201 : create
}, function(err) {
res.redirect(307, '/sendtoken');
}, function(err) {
console.log(err);
// registration failed
res.status(400).json(err);
})

})
// send token

router.post('/sendtoken',(req, res)=>{

twilio.sendAuthyToken(req.body,(response, authyId)=>{
// success callback
console.log(response);
user.authyId = authyId;
res.status(200).json('Token is sent on given phone');

},
(error)=>{
//error callback
console.log('error: ',error);
res.status(400).json('Token not sent, Error: '+JSON.stringify(error.message));
});
});
// varify token

router.post('/varifytoken', (req, res)=>{
console.log(user.authyId);
twilio.verifyAuthyToken(user.authyId, req.body.otp, (error, response)=>{
if(error){
console.log('Token varification error:',error);
res.status(400).json(JSON.stringify(error.message));

}
else{
console.log('Token varification successful: ',response);
res.status(200).json('Token varification successful: '+JSON.stringify(response.message));
}
})

});

router.get('/',(req, res)=>{
res.json('node is listening');

});
module.exports = router;
11 changes: 5 additions & 6 deletions server/twilio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@ const twilioClient = require('twilio')(config.accountSid, config.authToken);
let auth = {};

auth.sendAuthyToken = function(user, successCallback, errorCallback) {
let AuthyUser = { email: '[email protected]', cellphone: user.mobile, countryCode: user.countryCode}

if (!user.authyId) {
// Register this user if it's a new user
authy.register_user(AuthyUser.email, AuthyUser.cellphone, AuthyUser.countryCode,
authy.register_user('[email protected]', user.mobile, user.countryCode,
function(err, response) {
if (err || !response.user) return errorCallback(err);
user.authyId = response.user.id;
console.log('registering new user');
// successCallback(response, user.authyId);
sendToken();
});
} else {
// Otherwise send token to a known user
console.log('registered authy id');
console.log('registered authy user found');
sendToken();
}

Expand All @@ -28,7 +27,7 @@ let AuthyUser = { email: '[email protected]', cellphone: user.mobile, countryCode:
authy.request_sms(user.authyId, true, function(error, response) { //force : true
if(error) errorCallback(error);
else
successCallback(response, user);
successCallback(response, user.authyId);
});

}
Expand All @@ -37,7 +36,7 @@ let AuthyUser = { email: '[email protected]', cellphone: user.mobile, countryCode:
// Test a 2FA token
auth.verifyAuthyToken = function(authyId, otp , cb) {

authy.verify(179825188, otp, function(err, response) {
authy.verify(authyId, otp, function(err, response) {
cb(err, response);
});
};
Expand Down
4 changes: 3 additions & 1 deletion src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LoginComponent } from './login/login.component'
import { DashboardComponent } from './dashboard/dashboard.component';

const routes: Routes = [
{ path: '', redirectTo: '/login', pathMatch: 'full' },
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{path: 'dashboard', component: DashboardComponent}
];

@NgModule({
Expand Down
4 changes: 3 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import {HttpClientModule } from '@angular/common/http';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { TopBarComponent } from './top-bar/top-bar.component';
import { MyMaterialModule } from './material.module';
import { MyMaterialModule } from './helpers/material.module';
import { LoginComponent } from './login/login.component';
import { DashboardComponent } from './dashboard/dashboard.component';

@NgModule({
declarations: [
AppComponent,
TopBarComponent,
LoginComponent,
DashboardComponent,
],
imports: [
BrowserModule,
Expand Down
Empty file.
1 change: 1 addition & 0 deletions src/app/dashboard/dashboard.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>dashboard works!</p>
25 changes: 25 additions & 0 deletions src/app/dashboard/dashboard.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { DashboardComponent } from './dashboard.component';

describe('DashboardComponent', () => {
let component: DashboardComponent;
let fixture: ComponentFixture<DashboardComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ DashboardComponent ]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(DashboardComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
15 changes: 15 additions & 0 deletions src/app/dashboard/dashboard.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Component, OnInit } from '@angular/core';

@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {

constructor() { }

ngOnInit() {
}

}
File renamed without changes.
2 changes: 1 addition & 1 deletion src/app/login/login.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<tr>
<td></td>
<td>
<mat-error *ngIf="FLAG_ERROR">Invalid Fields</mat-error>
<mat-error #error></mat-error>
</td>
</tr>
</table>
Expand Down
37 changes: 24 additions & 13 deletions src/app/login/login.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, OnInit } from '@angular/core';
import { Component, OnInit, ViewChild, ElementRef} from '@angular/core';
import {Validators, FormBuilder} from '@angular/forms';
import { LoginService } from '../services/login.service';

Expand All @@ -8,9 +8,10 @@ import { LoginService } from '../services/login.service';
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
@ViewChild('error', {static: false}) error: ElementRef;

FLAG_VALID = false;
FLAG_OTP = false;
FLAG__ERROR = false;
form = this.fb.group({
mobile: ['', [Validators.required, Validators.min(1000000000), Validators.max(9999999999)]],
countryCode: ['+91', [Validators.required]],
Expand All @@ -19,38 +20,48 @@ export class LoginComponent implements OnInit {

onSubmit() {

if (!this.FLAG_VALID) {
console.log('Form is ' + this.FLAG_VALID);
}

if (this.formValidator()) {

this.loginService.login(this.form.value).subscribe(

this.loginService.login(this.form.value).subscribe(
response => {
if (response.status === 200) {
if (response) {
console.log(response.body);
this.getOtp();
this.error.nativeElement.innerHTML = response.body;
}
},
err => {
console.log('Error:', err.error);
// this.FLAG__ERROR = true;
this.error.nativeElement.innerHTML = err.error;
});
}
}
this.update_error();
this.getOtp();

} else { this.update_error(); }
}
// form Validator
formValidator() {
this.FLAG_VALID = (this.form.valid) && (this.form.get('mobile').valid);
return this.FLAG_VALID;
}
// otp
getOtp() {
setTimeout(() => {
this.FLAG_OTP = true;
this.form.addControl('otp', this.fb.control(''));
this.form.get('otp').setValidators([Validators.required]);
}
}, 200);
}
// error updates
update_error() {
if (!this.FLAG_VALID) {
console.log('Form is ' + this.FLAG_VALID);
this.error.nativeElement.innerHTML = 'Invalid Fields';
} else { this.error.nativeElement.innerHTML = ''; }

}
ngOnInit() {

}

}
2 changes: 2 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@
</head>
<body>
<app-root></app-root>
<script src="./socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
</body>
</html>

0 comments on commit d34558a

Please sign in to comment.