Skip to content

Commit 2b41bdf

Browse files
committed
Signin/signup Working
1 parent 42b5104 commit 2b41bdf

9 files changed

Lines changed: 98 additions & 32 deletions

File tree

codecamp-front/src/app/app-routing.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const appRoutes: Routes = [
2525
{path: 'contest' , component: ContestComponent},
2626
{path: 'discuss' , component: DiscussComponent},
2727
{path: 'profile' , component: ProfileComponent},
28-
{path: 'ide', component: IdeComponent}
28+
{path: 'ide', component: IdeComponent},
2929
];
3030

3131
@NgModule({

codecamp-front/src/app/components/login/login.component.html

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
<div class="container">
2-
<div class="row show-hide-message">
3-
<div [ngClass]="messageClass">
4-
{{ message }}
5-
</div>
6-
</div>
7-
8-
<form [formGroup]="form" (submit)="onSignupSubmit()">
2+
<form [formGroup]="form" (submit)="onSignInSubmit()">
93
<div class="form-group">
104
<label for="username">Username</label>
115
<input type="text"
@@ -31,6 +25,6 @@
3125

3226
<input
3327
[disabled]="!form.valid"
34-
type="submit" class="btn btn-primary" value="Sign up">
28+
type="submit" class="btn btn-primary" value="Sign In">
3529
</form>
3630
</div>
Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
23
import { AuthService } from '../../services/auth.service';
34
import { Router } from '@angular/router';
45

@@ -8,14 +9,41 @@ import { Router } from '@angular/router';
89
styleUrls: ['./login.component.css']
910
})
1011
export class LoginComponent implements OnInit {
11-
username: string;
12-
password: string;
12+
form: FormGroup;
13+
message;
14+
messageClass;
1315
constructor(
14-
private authService: AuthService,
15-
private router: Router
16-
) { }
16+
private authService: AuthService,
17+
private router: Router,
18+
private formBuilder: FormBuilder
19+
) {
20+
this.createForm();
21+
}
1722

1823
ngOnInit() {
1924
}
2025

26+
createForm() {
27+
this.form = this.formBuilder.group({
28+
password: ['', Validators.compose([
29+
Validators.required,
30+
Validators.minLength(8)
31+
])],
32+
username: ['', Validators.required]
33+
});
34+
}
35+
36+
onSignInSubmit() {
37+
const user = {
38+
username: this.form.get('username').value, // Username input field
39+
password: this.form.get('password').value // Password input field
40+
};
41+
this.authService.loginUser(user).subscribe(data => {
42+
this.authService.storeUserData(data.token, data.user);
43+
// After 2 seconds, redirect to dashboard page
44+
setTimeout(() => {
45+
this.router.navigate(['/']); // Navigate to dashboard view
46+
}, 2000);
47+
});
48+
}
2149
}

codecamp-front/src/app/components/navbar/navbar.component.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,29 @@
4848
<a class="nav-link"
4949
routerLink="/login"
5050
routerLinkActive="active"
51+
*ngIf="!authService.loggedIn()"
5152
>Login</a>
5253
</li>
5354

5455
<li class="nav-item">
5556
<a class="nav-link"
5657
routerLink="/profile"
5758
routerLinkActive="active"
59+
*ngIf="authService.loggedIn()"
5860
>My Profile</a>
5961
</li>
6062

6163
<li class="nav-item">
6264
<a class="nav-link"
6365
routerLink="/register"
6466
routerLinkActive="active"
67+
*ngIf="!authService.loggedIn()"
6568
>Register</a>
6669
</li>
6770
<li class="nav-item" >
6871
<a class="nav-link"
6972
(click)="onLogoutClick()"
70-
73+
*ngIf="authService.loggedIn()"
7174
>Logout</a>
7275
</li>
7376
</ul>

codecamp-front/src/app/components/register/register.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
</div>
110110

111111
<input
112-
[disabled]="!form.valid"
112+
[disabled]="!form.valid|| processing "
113113
type="submit" class="btn btn-primary" value="Sign up">
114114
</form>
115115
</div>

codecamp-front/src/app/components/register/register.component.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { Router } from '@angular/router';
1111
export class RegisterComponent implements OnInit {
1212

1313
form: FormGroup;
14-
message;
15-
messageClass;
14+
// message;
15+
// messageClass;
1616

1717
constructor(
1818
private authService: AuthService,
@@ -47,6 +47,8 @@ export class RegisterComponent implements OnInit {
4747
}, { validator: this.matchingPasswords('password', 'confirm')});
4848
}
4949

50+
51+
5052
validateEmail(controls) {
5153
// Create a regular expression
5254
const regExp = new RegExp(/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);
@@ -69,6 +71,7 @@ export class RegisterComponent implements OnInit {
6971
};
7072
}
7173
onSignupSubmit() {
74+
7275
const user = {
7376
username: this.form.get('username').value,
7477
email_id: this.form.get('email_id').value,
@@ -80,8 +83,10 @@ export class RegisterComponent implements OnInit {
8083
gender: this.form.get('gender').value
8184
};
8285
this.authService.registerUser(user).subscribe(data => {
83-
console.log(data);
86+
if (!data.success) {
87+
} else {
88+
this.router.navigate(['/login']);
89+
}
8490
});
85-
this.router.navigate(['/login']);
8691
}
8792
}
Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@angular/core';
2-
import {Http, Headers} from '@angular/http';
2+
import {Http, Headers, RequestOptions} from '@angular/http';
33
import { HttpModule } from '@angular/http';
44

55
import 'rxjs/add/operator/map';
@@ -9,14 +9,54 @@ import { tokenNotExpired } from 'angular2-jwt';
99
export class AuthService {
1010
authToken: any;
1111
user: any;
12-
domain = 'http://localhost:3000/';
12+
domain = 'http://localhost:3000';
13+
options;
1314

1415
constructor(
1516
private http: Http
1617
) { }
1718

19+
createAuthenticationHeaders() {
20+
this.loadToken(); // Get token so it can be attached to headers
21+
// Headers configuration options
22+
this.options = new RequestOptions({
23+
headers: new Headers({
24+
'Content-Type': 'application/json', // Format set to JSON
25+
'authorization': this.authToken // Attach token
26+
})
27+
});
28+
}
29+
30+
loadToken() {
31+
this.authToken = localStorage.getItem('token');
32+
console.log(this.authToken); // Get token and asssign to variable to be used elsewhere
33+
}
1834
registerUser(user) {
19-
return this.http.post(this.domain + 'users/signup', user)
35+
return this.http.post(this.domain + '/users/signup', user)
2036
.map(res => res.json());
2137
}
38+
39+
loginUser(user) {
40+
this.createAuthenticationHeaders();
41+
return this.http.post(this.domain + '/users/signin', user, this.options)
42+
.map(res => res.json());
43+
}
44+
45+
storeUserData(token, user) {
46+
47+
localStorage.setItem('token', token); // Set token in local storage
48+
localStorage.setItem('user', JSON.stringify(user)); // Set user in local storage as string
49+
this.authToken = token; // Assign token to be used elsewhere
50+
this.user = user; // Set user to be used elsewhere
51+
}
52+
53+
loggedIn() {
54+
return tokenNotExpired();
55+
}
56+
57+
logout() {
58+
this.authToken = null; // Set token to null
59+
this.user = null; // Set user to null
60+
localStorage.clear(); // Clear local storage
61+
}
2262
}

server/routes/users.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,11 @@ router.post('/signup',(req,res)=>{
2424
var token = jwt.sign({ username: user.username},'secret', {
2525
expiresIn: 86400 // expires in 24 hours
2626
});
27-
<<<<<<< HEAD
28-
res.json({ success: true, message: 'Acount registered!' });
29-
=======
3027
res.status(200).json({
3128
'success':true,
3229
'msg':'Registration successful , Now you can Login',
3330
'token':token
3431
})
35-
>>>>>>> 08e47d897437c805e78a5f0730556facf6bb31a6
3632
}, (e) => {
3733
res.status(400).json({
3834
'success':false,
@@ -95,7 +91,11 @@ router.post('/signin',(req,res)=>{
9591
});
9692
res.json({
9793
'success':true,
98-
'msg':'User with Username Exists'
94+
'msg':'Successfully logged In',
95+
'token': token,
96+
'user': {
97+
username: user.username
98+
}
9999
})
100100
}
101101
})

server/utils/db/db.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
const mongoose = require('mongoose')
22

33
mongoose.Promise = global.Promise
4-
<<<<<<< HEAD
54
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/OnlineJudge')
6-
=======
7-
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/OnlineJudge',{'useMongoClient':true})
8-
>>>>>>> 08e47d897437c805e78a5f0730556facf6bb31a6
95
module.exports = {
106
mongoose
117
}

0 commit comments

Comments
 (0)