-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add authentication by user vs client user
- Loading branch information
1 parent
e2796cc
commit 734b481
Showing
12 changed files
with
155 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface Account { | ||
user: string, | ||
email: string, | ||
password: string, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface ClientUser { | ||
id: string, | ||
name: string, | ||
token: string, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface Token { | ||
jwt: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
export interface User { | ||
id: string, | ||
name: string, | ||
token: string, | ||
name: string, | ||
domain: string, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import {Injectable} from '@angular/core'; | ||
import {Account, Token, User} from '../entities'; | ||
import {HttpClient, HttpHeaders} from '@angular/common/http'; | ||
import {ParameterService} from './parameter.service'; | ||
import {catchError, lastValueFrom, map, mergeMap, Observable, tap} from 'rxjs'; | ||
import {handleError} from './error.handler'; | ||
import {SessionService} from './session.service'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class AuthService { | ||
|
||
httpOptions = { | ||
headers: new HttpHeaders({'Content-Type': 'application/json', 'Accept': 'application/json'}), | ||
}; | ||
|
||
constructor(private http: HttpClient, private params: ParameterService, private session: SessionService) { | ||
} | ||
|
||
login(email: string, pass: string): Observable<void> { | ||
const loginUrl = `${this.params.API_PREFIX}/auth/login`; | ||
const body = {email, pass}; | ||
// returns 200 || 403 | ||
return this.http.post<Token>(loginUrl, body, this.httpOptions).pipe( | ||
tap((token: Token) => this.session.setAuthenticationToken(token.jwt)), | ||
mergeMap(() => this.getUser()), | ||
map((user) => this.session.setUser(user)) | ||
); | ||
} | ||
|
||
registerAccount(account: Account): Observable<void> { | ||
const url = `${this.params.API_PREFIX}/auth/register`; | ||
// returns 201 || 400 | ||
return this.http.post<void>(url, account, this.httpOptions); | ||
} | ||
|
||
verifyAccount(token: String): Observable<void> { | ||
const url = `${this.params.API_PREFIX}/auth/verify`; | ||
// returns 200 || 400 | ||
return this.http.put<void>(url, {token}, this.httpOptions); | ||
} | ||
|
||
forgetPassword(email: string) { | ||
const url = `${this.params.API_PREFIX}/auth/forgotPassword`; | ||
const body = {email}; | ||
|
||
return lastValueFrom(this.http.post(url, body, this.httpOptions).pipe( | ||
tap(a => console.log('Forgot Password Finish', a)), | ||
catchError(handleError<string>('forgot password', '')) | ||
) | ||
); | ||
} | ||
|
||
newPassword(password: string, token: string) { | ||
const url = `${this.params.API_PREFIX}/auth/newPassword`; | ||
const body = {password, token}; | ||
|
||
return lastValueFrom(this.http.post(url, body, this.httpOptions).pipe( | ||
tap(a => console.log('New Password Finish', a)), | ||
catchError(handleError<string>('new password', '')) | ||
) | ||
); | ||
} | ||
|
||
deleteAccount(email: string) { | ||
const url = `${this.params.API_PREFIX}/auth/deleteAccount`; | ||
const body = {email}; | ||
|
||
return lastValueFrom(this.http.post(url, body, this.httpOptions).pipe( | ||
tap(a => console.log('Delete Account', a)), | ||
catchError(handleError<string>('delete account', '')) | ||
) | ||
); | ||
} | ||
|
||
private getUser(): Observable<User> { | ||
const userUrl = `${this.params.API_PREFIX}/auth/user`; | ||
// returns 200 || 403 | ||
return this.http.get<User>(userUrl, this.httpOptions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {Observable, of} from 'rxjs'; | ||
|
||
|
||
/** | ||
* Handle Http operation that failed. | ||
* Let the app continue. | ||
* | ||
* @param operation - name of the operation that failed | ||
* @param result - optional value to return as the observable result | ||
*/ | ||
export const handleError = <T>(operation = 'operation', result?: T): (error: any) => Observable<T> => { | ||
|
||
return (error: any): Observable<T> => { | ||
|
||
// TODO: send the error to remote logging infrastructure | ||
console.error(error); // log to console instead | ||
|
||
// TODO: better job of transforming error for user consumption | ||
console.log(`${operation} failed: ${error.message}`); | ||
|
||
// Let the app keep running by returning an empty result. | ||
return of(result as T); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,58 @@ | ||
import {Injectable} from '@angular/core'; | ||
import {BehaviorSubject, Observable, of, Subject} from 'rxjs'; | ||
import {User} from '../entities'; | ||
import {ClientUser} from '../entities'; | ||
import {User} from '../entities/user'; | ||
|
||
const USER_NAME_KEY = 'user-name'; | ||
const USER_DOMAIN_KEY = 'user-domain'; | ||
const SESSION_TOKEN_KEY = 'jwt'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class SessionService { | ||
private readonly users: User[] = [ | ||
{id: 'root@localhost:9000', name: 'root@localhost:9000', token: token1}, | ||
{id: 'user123@localhost:9000', name: 'user123@localhost:9000', token: token2}, | ||
{id: 'guest-2', name: 'Guest 2', token: token3}, | ||
{id: 'guest-3', name: 'Guest 3', token: token4}, | ||
{id: 'guest-4', name: 'Guest 4', token: token5}, | ||
]; | ||
|
||
private readonly userName$: Subject<string>; | ||
private readonly anonymous = 'anonymous'; | ||
|
||
private authToken: string | undefined; | ||
|
||
constructor() { | ||
const user = localStorage.getItem('user'); | ||
const user = window.localStorage.getItem(USER_NAME_KEY); | ||
this.userName$ = new BehaviorSubject<string>(user === null ? this.anonymous : user); | ||
} | ||
|
||
setAuthenticationToken(token: string) { | ||
this.authToken = token; | ||
public setAuthenticationToken(token: string) { | ||
window.localStorage.setItem(SESSION_TOKEN_KEY, token); | ||
} | ||
|
||
public getAuthenticationToken(): string | null { | ||
return window.localStorage.getItem(SESSION_TOKEN_KEY); | ||
} | ||
|
||
isActive(): Observable<boolean> { | ||
if (this.authToken !== undefined) { | ||
if (this.getAuthenticationToken() !== null) { | ||
return of(true); | ||
} | ||
const user = localStorage.getItem('user'); | ||
return of(user !== null); | ||
return of(false) | ||
} | ||
|
||
getUserName(): Observable<string> { | ||
return this.userName$; | ||
} | ||
|
||
setUserName(user: User): boolean { | ||
const found = this.users.find((list) => list.id === user.id); | ||
if (found === undefined) { | ||
return false; | ||
} | ||
localStorage.setItem('user', user.name); | ||
this.userName$.next(user.name); | ||
return true; | ||
} | ||
|
||
getUsers(): User[] { | ||
return this.users; | ||
} | ||
|
||
public removeUser(key: string) { | ||
localStorage.removeItem('user'); | ||
this.userName$.next(this.anonymous); | ||
} | ||
|
||
public clearData() { | ||
localStorage.clear(); | ||
window.localStorage.clear(); | ||
this.userName$.next(this.anonymous); | ||
} | ||
|
||
public getToken(): string { | ||
if (this.authToken !== undefined) { | ||
return this.authToken; | ||
} | ||
|
||
let userName = localStorage.getItem('user'); | ||
if (userName === null) { | ||
return ''; | ||
} | ||
const user = this.users.find(items => items.name === userName); | ||
if (user === undefined) { | ||
return ''; | ||
} | ||
public setUser(user: User) { | ||
window.localStorage.setItem(USER_NAME_KEY, user.name); | ||
window.localStorage.setItem(USER_DOMAIN_KEY, user.domain); | ||
this.userName$.next(user.name); | ||
} | ||
|
||
return `Bearer ${user.token}`; | ||
public removeUser(key: string) { | ||
window.localStorage.removeItem(USER_NAME_KEY); | ||
window.localStorage.removeItem(USER_DOMAIN_KEY); | ||
this.userName$.next(this.anonymous); | ||
} | ||
} | ||
|
||
const token1 = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNTE2MjM5MDIyLCJ1dWlkIjoiMWY1NjA2NmYtYjA1Yi00NDhhLWI3NjUtYjhkYzJiZTU1OTY3In0.GV0ptLGhnA0gwJC5zlKPY5z94KfLYUEpDraQmXPAR4o'; | ||
const token2 = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNTE2MjM5MDIyLCJ1dWlkIjoiOGRhMGNkYzItZTVmMS00ZmZiLWIwYTktYWYxODI0MzI5OTQ0In0.CVk4I_9BP5yQuFS4X6XNZsvQ7tFStn3eGXGtNVYvSjY'; | ||
const token3 = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNTE2MjM5MDIyLCJ1dWlkIjoiNzBiZTg2ODItNzk5Yy00MjdmLWI3MjgtZmQwMDhjNTYzYWFjIn0.mLT7DRp50QP6OqqxBQmf4VSx02i3cA0jk89UMdXLhBY'; | ||
const token4 = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNTE2MjM5MDIyLCJ1dWlkIjoiMjhiYmYzNTctYTFmNy00NDkwLWIxZjItYTYzN2E0YWU5YmFlIn0.wMfmkJ0VBj86tW5NfQnnV91j2YT-mUZeM_E9qbt_bjg'; | ||
const token5 = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNTE2MjM5MDIyLCJ1dWlkIjoiZmUwMjI2NDgtYTBlOS00NDQ0LTlkNGQtYTRjMGQ5ZWZiNmQ3In0.HgWacVwFeEgYBG6iDJYlQ25VTymM0xHpnppjWGrzOp4'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters