Skip to content

LoginUpdates #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43,222 changes: 29,884 additions & 13,338 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions src/app/_helpers/fake-backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ export class FakeBackendInterceptor implements HttpInterceptor {

function handleRoute() {
switch (true) {
case url.endsWith('/users/authenticate') && method === 'POST':
case url.endsWith('/authenticate') && method === 'POST':
return authenticate();
case url.endsWith('/users/register') && method === 'POST':
case url.endsWith('/register') && method === 'POST':
return register();
case url.endsWith('/users') && method === 'GET':
case url.endsWith('/') && method === 'GET':
return getUsers();
case url.match(/\/users\/\d+$/) && method === 'GET':
case url.match(/\/\d+$/) && method === 'GET':
return getUserById();
case url.match(/\/users\/\d+$/) && method === 'PUT':
case url.match(/\/\d+$/) && method === 'PUT':
return updateUser();
case url.match(/\/users\/\d+$/) && method === 'DELETE':
case url.match(/\/\d+$/) && method === 'DELETE':
return deleteUser();
default:
// pass through any requests not handled above
return next.handle(request);
}
}
}

// route functions
Expand Down Expand Up @@ -135,4 +135,4 @@ export const fakeBackendProvider = {
provide: HTTP_INTERCEPTORS,
useClass: FakeBackendInterceptor,
multi: true
};
};
4 changes: 3 additions & 1 deletion src/app/_models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
password: string;
firstName: string;
lastName: string;
email : string;
confirmPassword : string;
token: string;
}
}
20 changes: 13 additions & 7 deletions src/app/_services/account.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { BehaviorSubject, Observable } from 'rxjs';
import { map } from 'rxjs/operators';

Expand All @@ -25,7 +25,13 @@ export class AccountService {
}

login(username, password) {
return this.http.post<User>(`${environment.apiUrl}/users/authenticate`, { username, password })

var data = "grant_type=password" + "&username=" + username + "&password=" + password ;
var reqHeader = new HttpHeaders({ 'Content-Type': 'application/X-www-urlencoded'});
// return this.http.post(this.rootUrl + '/token', data, { headers: reqHeader });
console.log(data);
console.log(reqHeader);
return this.http.post<User>(`https://localhost:44399/Token`, data, { headers: reqHeader })
.pipe(map(user => {
// store user details and jwt token in local storage to keep user logged in between page refreshes
localStorage.setItem('user', JSON.stringify(user));
Expand All @@ -42,19 +48,19 @@ export class AccountService {
}

register(user: User) {
return this.http.post(`${environment.apiUrl}/users/register`, user);
return this.http.post(`${environment.apiUrl}/Register`, user);
}

getAll() {
return this.http.get<User[]>(`${environment.apiUrl}/users`);
}

getById(id: string) {
return this.http.get<User>(`${environment.apiUrl}/users/${id}`);
return this.http.get<User>(`${environment.apiUrl}/${id}`);
}

update(id, params) {
return this.http.put(`${environment.apiUrl}/users/${id}`, params)
return this.http.put(`${environment.apiUrl}/${id}`, params)
.pipe(map(x => {
// update stored user if the logged in user updated their own record
if (id == this.userValue.id) {
Expand All @@ -70,7 +76,7 @@ export class AccountService {
}

delete(id: string) {
return this.http.delete(`${environment.apiUrl}/users/${id}`)
return this.http.delete(`${environment.apiUrl}/${id}`)
.pipe(map(x => {
// auto logout if the logged in user deleted their own record
if (id == this.userValue.id) {
Expand All @@ -79,4 +85,4 @@ export class AccountService {
return x;
}));
}
}
}
11 changes: 6 additions & 5 deletions src/app/account/login.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ <h4 class="card-header">Login</h4>
<div class="card-body">
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<div class="form-group">
<label for="username">Username</label>
<input type="text" formControlName="username" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.username.errors }" />
<div *ngIf="submitted && f.username.errors" class="invalid-feedback">
<div *ngIf="f.username.errors.required">Username is required</div>
<label for="username">Email</label>
<input type="text" formControlName="email" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.email.errors }" />
<div *ngIf="submitted && f.email.errors" class="invalid-feedback">
<div *ngIf="f.email.errors.required">Email is required</div>
<div *ngIf="f.email.errors.email">Valid Email is required</div>
</div>
</div>
<div class="form-group">
Expand All @@ -25,4 +26,4 @@ <h4 class="card-header">Login</h4>
</div>
</form>
</div>
</div>
</div>
6 changes: 3 additions & 3 deletions src/app/account/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class LoginComponent implements OnInit {

ngOnInit() {
this.form = this.formBuilder.group({
username: ['', Validators.required],
email: ['', [Validators.required, Validators.email]],
password: ['', Validators.required]
});
}
Expand All @@ -41,7 +41,7 @@ export class LoginComponent implements OnInit {
}

this.loading = true;
this.accountService.login(this.f.username.value, this.f.password.value)
this.accountService.login(this.f.email.value, this.f.password.value)
.pipe(first())
.subscribe({
next: () => {
Expand All @@ -55,4 +55,4 @@ export class LoginComponent implements OnInit {
}
});
}
}
}
32 changes: 22 additions & 10 deletions src/app/account/register.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,46 @@ <h4 class="card-header">Register</h4>
<div class="card-body">
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<div class="form-group">
<label for="firstName">First Name</label>
<input type="text" formControlName="firstName" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.firstName.errors }" />
<div *ngIf="submitted && f.firstName.errors" class="invalid-feedback">
<div *ngIf="f.firstName.errors.required">First Name is required</div>
<label for="firstName">Email</label>
<input type="text" formControlName="email" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.email.errors }" />
<div *ngIf="submitted && f.email.errors" class="invalid-feedback">
<div *ngIf="f.email.errors.required">Email is required</div>
<div *ngIf="f.email.errors.email">Enter the Valid Email</div>
</div>
</div>
<div class="form-group">

<!-- <div class="form-group">
<label for="lastName">Last Name</label>
<input type="text" formControlName="lastName" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.lastName.errors }" />
<div *ngIf="submitted && f.lastName.errors" class="invalid-feedback">
<div *ngIf="f.lastName.errors.required">Last Name is required</div>
</div>
</div>
<div class="form-group">
</div> -->
<!-- <div class="form-group">
<label for="username">Username</label>
<input type="text" formControlName="username" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.username.errors }" />
<div *ngIf="submitted && f.username.errors" class="invalid-feedback">
<div *ngIf="f.username.errors.required">Username is required</div>
</div>
</div>
</div> -->
<div class="form-group">
<label for="password">Password</label>
<input type="password" formControlName="password" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.password.errors }" />
<div *ngIf="submitted && f.password.errors" class="invalid-feedback">
<div *ngIf="f.password.errors.required">Password is required</div>
<div *ngIf="f.password.errors.minlength">Password must be at least 6 characters</div>
<div *ngIf="f.password.errors.minlength">Password must be at least 8 characters</div>

</div>
</div>
<div class="form-group">
<label for="password">Confirm Password</label>
<input type="password" formControlName="confirmPassword" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.confirmPassword.errors }" />
<div *ngIf="submitted && f.confirmPassword.errors" class="invalid-feedback">
<div *ngIf="f.confirmPassword.errors.required">Confirm Password is required</div>
<div *ngIf="f.confirmPassword.errors.minlength">Confirm Password must be at least 8 characters</div>

</div>
</div>
<div class="form-group">
<button [disabled]="loading" class="btn btn-primary">
<span *ngIf="loading" class="spinner-border spinner-border-sm mr-1"></span>
Expand All @@ -40,4 +52,4 @@ <h4 class="card-header">Register</h4>
</div>
</form>
</div>
</div>
</div>
15 changes: 7 additions & 8 deletions src/app/account/register.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { Router, ActivatedRoute } from '@angular/router';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { first } from 'rxjs/operators';

import { AccountService, AlertService } from '@app/_services';

@Component({ templateUrl: 'register.component.html' })
Expand All @@ -21,22 +20,22 @@ export class RegisterComponent implements OnInit {

ngOnInit() {
this.form = this.formBuilder.group({
firstName: ['', Validators.required],
lastName: ['', Validators.required],
username: ['', Validators.required],
password: ['', [Validators.required, Validators.minLength(6)]]
});
email: ['', [Validators.required, Validators.email]],
password: ['', [Validators.required, Validators.minLength(8)]],
confirmPassword: ['',[Validators.required, Validators.minLength(8)]]
}
);
}

// convenience getter for easy access to form fields
get f() { return this.form.controls; }

onSubmit() {
this.submitted = true;

// reset alerts on submit
this.alertService.clear();

// this.alertService.error("Password and Confirm Password Did not Match");
// stop here if form is invalid
if (this.form.invalid) {
return;
Expand All @@ -56,4 +55,4 @@ export class RegisterComponent implements OnInit {
}
});
}
}
}
10 changes: 0 additions & 10 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,3 @@
<alert></alert>
<router-outlet></router-outlet>
</div>

<!-- credits -->
<div class="text-center mt-4">
<p>
<a href="https://jasonwatmore.com/post/2020/07/18/angular-10-user-registration-and-login-example-tutorial" target="_top">Angular 10 - User Registration and Login Example & Tutorial</a>
</p>
<p>
<a href="https://jasonwatmore.com" target="_top">JasonWatmore.com</a>
</p>
</div>
4 changes: 2 additions & 2 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import { HomeComponent } from './home';
{ provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true },

// provider used to create fake backend
fakeBackendProvider
// fakeBackendProvider
],
bootstrap: [AppComponent]
})
export class AppModule { };
export class AppModule { };
2 changes: 1 addition & 1 deletion src/environments/environment.prod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const environment = {
production: true,
apiUrl: 'http://localhost:4000'
apiUrl: 'https://localhost:44313/api/Account'
};
2 changes: 1 addition & 1 deletion src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

export const environment = {
production: false,
apiUrl: 'http://localhost:4000'
apiUrl: 'https://localhost:44399/api/Account'
};

/*
Expand Down