-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathauth.in-memory.service.ts
86 lines (75 loc) · 2.17 KB
/
auth.in-memory.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { Injectable } from '@angular/core'
import { sign } from 'fake-jwt-sign' // For InMemoryAuthService only
import { Observable, of, throwError } from 'rxjs'
import { PhoneType, User } from '../user/user/user'
import { Role } from './auth.enum'
import { AuthService, IAuthStatus, IServerAuthResponse } from './auth.service'
@Injectable({
providedIn: 'root',
})
export class InMemoryAuthService extends AuthService {
// LemonMart Server User Id: 5da01751da27cc462d265913
private defaultUser = User.Build({
_id: '5da01751da27cc462d265913',
email: 'duluca@gmail.com',
name: { first: 'Doguhan', last: 'Uluca' },
picture: 'https://secure.gravatar.com/avatar/7cbaa9afb5ca78d97f3c689f8ce6c985',
role: Role.Manager,
dateOfBirth: new Date(1980, 1, 1),
userStatus: true,
address: {
line1: '101 Sesame St.',
city: 'Bethesda',
state: 'Maryland',
zip: '20810',
},
level: 2,
phones: [
{
id: 0,
type: PhoneType.Mobile,
digits: '5555550717',
},
],
})
constructor() {
super()
console.warn(
"You're using the InMemoryAuthService. Do not use this service in production."
)
}
protected authProvider(
email: string,
_password: string
): Observable<IServerAuthResponse> {
email = email.toLowerCase()
if (!email.endsWith('@test.com')) {
return throwError(() => 'Failed to login! Email needs to end with @test.com.')
}
const authStatus = {
isAuthenticated: true,
userId: this.defaultUser._id,
userRole: email.includes('cashier')
? Role.Cashier
: email.includes('clerk')
? Role.Clerk
: email.includes('manager')
? Role.Manager
: Role.None,
} as IAuthStatus
this.defaultUser.role = authStatus.userRole
const authResponse = {
accessToken: sign(authStatus, 'secret', {
expiresIn: '1h',
algorithm: 'none',
}),
} as IServerAuthResponse
return of(authResponse)
}
protected transformJwtToken(token: IAuthStatus): IAuthStatus {
return token
}
protected getCurrentUser(): Observable<User> {
return of(this.defaultUser)
}
}