added profile section in sidenav and changed auth

urls
FlayInAHook 4 years ago
parent f3982dd79d
commit 58a3f76a3c

@ -29,6 +29,12 @@
<mat-sidenav-container id="content"> <mat-sidenav-container id="content">
<mat-sidenav #appDrawer mode="over" opened="false"> <mat-sidenav #appDrawer mode="over" opened="false">
<mat-nav-list class="nav-profile-list">
<mat-list-item class="nav-profile-list-item">
<sidenav-profile>
</sidenav-profile>
</mat-list-item>
</mat-nav-list>
<mat-nav-list> <mat-nav-list>
<app-menu-list-item *ngFor="let item of navItems" [item]="item"></app-menu-list-item> <app-menu-list-item *ngFor="let item of navItems" [item]="item"></app-menu-list-item>
</mat-nav-list> </mat-nav-list>

@ -1,3 +1,5 @@
@import '../theme';
#page-wrapper { #page-wrapper {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
@ -15,3 +17,14 @@
} }
} }
} }
.nav-profile-list {
padding-top:0px
}
mat-nav-list .nav-profile-list-item {
height:200px;
background: mat-color($light-primary, 300);;
//color:white;
}

@ -41,13 +41,16 @@ import { LendingStationsComponent } from './pages/tables/lending-stations/lendin
import { TableOverviewComponent } from './pages/table-overview/table-overview.component'; import { TableOverviewComponent } from './pages/table-overview/table-overview.component';
import { CellComponent } from './components/tableComponents/cell/cell.component'; import { CellComponent } from './components/tableComponents/cell/cell.component';
import { MenuListItemComponent } from './components/menu-list-item/menu-list-item.component'; import { MenuListItemComponent } from './components/menu-list-item/menu-list-item.component';
import {SidenavProfileComponent} from './components/sidenav-profile/sidenav-profile.component';
import { NavService }from './components/menu-list-item/nav.service'; import { NavService }from './components/menu-list-item/nav.service';
import { TokenInterceptor } from './helper/token.interceptor' import { TokenInterceptor } from './helper/token.interceptor'
@NgModule({ @NgModule({
declarations: [ declarations: [
AppComponent, AppComponent,
MenuListItemComponent, MenuListItemComponent,
SidenavProfileComponent,
LoginComponent, LoginComponent,
BikesComponent, BikesComponent,
ParticipantsComponent, ParticipantsComponent,

@ -25,7 +25,8 @@ export class MenuListItemComponent implements OnInit {
@Input() depth: number; @Input() depth: number;
constructor(public navService: NavService, constructor(public navService: NavService,
public router: Router) { public router: Router,
) {
if (this.depth === undefined) { if (this.depth === undefined) {
this.depth = 0; this.depth = 0;
} }

@ -0,0 +1,3 @@
<img class="navbar-photo" src={{profileURL}}>
<p>{{name}}</p>
<p>{{email}}</p>

@ -0,0 +1,8 @@
@import '../../../theme';
.navbar-photo {
width:100px;
height:100px;
border-radius:50%;
margin-top:10px;
}

@ -0,0 +1,23 @@
import { Component, OnInit } from '@angular/core';
import { AuthService } from '../../services/auth.service';
@Component({
selector: 'sidenav-profile',
templateUrl: './sidenav-profile.component.html',
styleUrls: ['./sidenav-profile.component.scss']
})
export class SidenavProfileComponent implements OnInit {
name: String;
email: String;
profileURL: String;
constructor( private auth: AuthService) { }
ngOnInit() {
this.auth.currentUser.subscribe(user => {
this.name = user.user.name;
this.email = user.user.email;
this.profileURL = user.user.attributes.profile_url;
});
}
}

@ -1,5 +1,13 @@
export class User { export class User {
request_token: string;
refresh_token: string;
user: {
id: number;
name: string;
email: string; email: string;
requestToken: string; attributes : {
refreshToken: string; profile_url: string;
}
}
} }

@ -2,20 +2,38 @@ import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { catchError, finalize, map, tap } from 'rxjs/operators'; import { catchError, finalize, map, tap } from 'rxjs/operators';
import { environment } from '../../environments/environment'; import { environment } from '../../environments/environment';
import { BehaviorSubject, of } from 'rxjs'; import { Observable, BehaviorSubject, of } from 'rxjs';
import { User } from "../models/user";
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
}) })
export class AuthService { export class AuthService {
private currentUserSubject: BehaviorSubject<User>;
public currentUser: Observable<User>;
public loggedIn: BehaviorSubject<boolean>; public loggedIn: BehaviorSubject<boolean>;
private readonly REQUEST_TOKEN = 'requestToken'; private readonly REQUEST_TOKEN = 'requestToken';
private readonly REFRESH_TOKEN = 'refreshToken'; private readonly REFRESH_TOKEN = 'refreshToken';
private readonly EMAIL = 'email'; private readonly CURRENT_USER = 'currentUser';
constructor(private http: HttpClient) { constructor(private http: HttpClient) {
this.currentUserSubject = new BehaviorSubject<User>(
JSON.parse(localStorage.getItem(this.CURRENT_USER))
);
this.currentUser = this.currentUserSubject.asObservable();
this.loggedIn = new BehaviorSubject<boolean>(false); this.loggedIn = new BehaviorSubject<boolean>(false);
this.checkIfUserIsLoggedIn(); this.checkIfUserIsLoggedIn();
}
public get getCurrentUserValue(): User {
var value = this.currentUserSubject.value;
if (value === null){
value = new User();
}
return value;
} }
private checkIfUserIsLoggedIn(): void { private checkIfUserIsLoggedIn(): void {
@ -23,11 +41,11 @@ export class AuthService {
} }
public getRequestToken(): string { public getRequestToken(): string {
return localStorage.getItem(this.REQUEST_TOKEN); return this.getCurrentUserValue.request_token;
} }
public getRefreshToken(): string { public getRefreshToken(): string {
return localStorage.getItem(this.REFRESH_TOKEN); return this.getCurrentUserValue.refresh_token;
} }
login(email: string, password: string) { login(email: string, password: string) {
@ -36,9 +54,11 @@ export class AuthService {
.pipe( .pipe(
map((response) => { map((response) => {
// store request and refresh token in local storage to keep user logged in between page refreshes // store request and refresh token in local storage to keep user logged in between page refreshes
this.storeTokens(response); if (response && response.request_token){
this.storeUser(response);
}
this.checkIfUserIsLoggedIn(); this.checkIfUserIsLoggedIn();
this.storeEmail(email); //this.storeTokens(response);
}) })
); );
} }
@ -57,14 +77,6 @@ export class AuthService {
); );
} }
storeEmail(email: string) {
localStorage.setItem(this.EMAIL, email);
}
getEmail() {
localStorage.getItem(this.EMAIL);
}
refreshToken() { refreshToken() {
return this.http return this.http
.post<any>(`${environment.authUrl}/new-token`, { .post<any>(`${environment.authUrl}/new-token`, {
@ -90,13 +102,19 @@ export class AuthService {
} }
private storeTokens(tokens: any) { private storeTokens(tokens: any) {
localStorage.setItem(this.REQUEST_TOKEN, tokens.request_token); this.storeUser({...this.getCurrentUserValue, ...tokens}); //Merging objects. Properties in obj2 will overwrite those in obj1
localStorage.setItem(this.REFRESH_TOKEN, tokens.refresh_token); //localStorage.setItem(this.REQUEST_TOKEN, tokens.request_token);
//localStorage.setItem(this.REFRESH_TOKEN, tokens.refresh_token);
}
private storeUser(usr: User){
localStorage.setItem(this.CURRENT_USER, JSON.stringify(usr));
this.currentUserSubject.next(usr);
} }
private removeTokens() { private removeTokens() {
localStorage.removeItem(this.REQUEST_TOKEN); //localStorage.removeItem(this.REQUEST_TOKEN);
localStorage.removeItem(this.REFRESH_TOKEN); //localStorage.removeItem(this.REFRESH_TOKEN);
localStorage.removeItem(this.EMAIL) localStorage.removeItem(this.CURRENT_USER);
} }
} }

Loading…
Cancel
Save