Skip to content
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
18 changes: 16 additions & 2 deletions src/app/events-app.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { AuthService } from './user/auth.service';

@Component({
selector: 'app-root',
Expand All @@ -18,6 +19,19 @@ import { Component } from '@angular/core';
// selector: 'app-root',
// templateUrl:'events-app-component.html',
// })
export class EventsAppComponent {
export class EventsAppComponent{
title = 'our event website';


constructor(private auth: AuthService){}



ngOnInit(): void {
this.auth.checkAuthenticationStatus().subscribe();
}




}
76 changes: 60 additions & 16 deletions src/app/user/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,72 @@
import { HttpClient, HttpHeaders } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { of } from "rxjs";
import { catchError, tap } from "rxjs/operators";
import { IUser } from "./user.model";

@Injectable()
export class AuthService{

currentUser!: IUser;

currentUser: IUser;


constructor(private http: HttpClient){}



loginUser(userName:string, password: string){
this.currentUser={
id:1,
firstName: 'Heba',
lastName: 'Almomani',
userName: userName,

let options ={headers: new HttpHeaders({'Content-Type': 'application/json'})}
let loginInfo = {username: userName, password: password }

return this.http.post('/api/login', loginInfo, options)
.pipe(tap(data =>{
this.currentUser= <IUser>data['user'];

}))
.pipe(catchError(err => {
return of(false)
}))
// this.currentUser={
// id:1,
// firstName: 'Heba',
// lastName: 'Almomani',
// userName: userName,
// }

}

isAuthenticated(){
return !!this.currentUser;
}


checkAuthenticationStatus() {
return this.http.get('/api/currentIdentity').pipe(tap(
data =>{
if(data instanceof Object){
this.currentUser = <IUser>data;
}
}
))

}

updateCurrentUser(firstname: string, lastname: string){
this.currentUser.firstName = firstname;
this.currentUser.lastName = lastname;

}
let options ={headers: new HttpHeaders({'Content-Type': 'application/json'})}
return this.http.put(`api/users/${this.currentUser.id}`, this.currentUser, options);

isAuthenticated(){
return !!this.currentUser;
}
}



updateCurrentUser(firstname: string, lastname: string){
this.currentUser.firstName = firstname;
this.currentUser.lastName = lastname;
}
}
logout(){
this.currentUser= undefined;
let options ={headers: new HttpHeaders({'Content-Type': 'application/json'})}
return this.http.post('api/logout', {}, options);

}
}
4 changes: 4 additions & 0 deletions src/app/user/login.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ <h1>Login</h1>
</span>
<button type="button" class="btn btn-default" (click)="cancel()">Cancel</button>
</form>
<br>
<div *ngIf="loginInvalid" class="alert alert-danger">
Invalid login information
</div>
</div>
27 changes: 17 additions & 10 deletions src/app/user/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,25 @@ export class LoginComponent{
userName: any
password: any
mouseoverlogin: boolean = false;
constructor(private authService: AuthService , private route: Router){
loginInvalid = false;

}
constructor(private authService: AuthService , private router: Router){}


login(formValue: any){
this.authService.loginUser(formValue.userName, formValue.password)
this.route.navigate(['/events'])
console.log(formValue.userName, formValue.password)
}
login(formValue:any){
this.authService.loginUser(formValue.userName, formValue.password).subscribe(resp =>{
if(!resp){
this.loginInvalid = true;
}else{
this.router.navigate(['events'])
}
})
// this.router.navigate(['events'])
// console.log(formValue.userName, formValue.password)
}


cancel(){
this.route.navigate(['/events'])
}
cancel(){
this.router.navigate(['events'])
}
}
1 change: 1 addition & 0 deletions src/app/user/profile/profile.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ <h1>Edit Your Profile </h1>
<button type="submit" class="btn btn-primary" [disabled]="profileForm.invalid">Save</button>
</span>
<button type="button" class="btn btn-default" (click)="canceled()">Cancel</button>
<button type="button" class="btn btn-warning" style="float: right;" (click)="logout()">Log out</button>
</form>
</div>
</div>
15 changes: 12 additions & 3 deletions src/app/user/profile/profile.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ export class ProfileComponent implements OnInit {

saveprofile(value: any){
if (this.profileForm.valid) {
this.auth.updateCurrentUser(value.firstName, value.lastName)
this.route.navigate(['/events'])
this.toastr.success('profile saved ')
this.auth.updateCurrentUser(value.firstName, value.lastName).subscribe(() =>{
this.route.navigate(['/events'])
this.toastr.success('profile saved ')
})
}
}

Expand All @@ -63,4 +64,12 @@ export class ProfileComponent implements OnInit {
validatedFirstName(){
return this.firstName.valid || this.firstName.untouched && !this.mouseover
}



logout(){
this.auth.logout().subscribe(() =>{
this.route.navigate(['/user/login'])
})
}
}