diff options
Diffstat (limited to 'client/src/app/core/auth')
-rw-r--r-- | client/src/app/core/auth/auth-user.model.ts | 11 | ||||
-rw-r--r-- | client/src/app/core/auth/auth.service.ts | 8 | ||||
-rw-r--r-- | client/src/app/core/auth/index.ts | 2 | ||||
-rw-r--r-- | client/src/app/core/auth/login-guard.service.ts | 30 |
4 files changed, 10 insertions, 41 deletions
diff --git a/client/src/app/core/auth/auth-user.model.ts b/client/src/app/core/auth/auth-user.model.ts index 81bff99a0..085b763ec 100644 --- a/client/src/app/core/auth/auth-user.model.ts +++ b/client/src/app/core/auth/auth-user.model.ts | |||
@@ -1,6 +1,7 @@ | |||
1 | // Do not use the barrel (dependency loop) | 1 | // Do not use the barrel (dependency loop) |
2 | import { UserRole } from '../../../../../shared/models/users/user-role.type' | 2 | import { hasUserRight, UserRole } from '../../../../../shared/models/users/user-role' |
3 | import { User, UserConstructorHash } from '../../shared/users/user.model' | 3 | import { User, UserConstructorHash } from '../../shared/users/user.model' |
4 | import { UserRight } from '../../../../../shared/models/users/user-right.enum' | ||
4 | 5 | ||
5 | export type TokenOptions = { | 6 | export type TokenOptions = { |
6 | accessToken: string | 7 | accessToken: string |
@@ -81,7 +82,7 @@ export class AuthUser extends User { | |||
81 | id: parseInt(localStorage.getItem(this.KEYS.ID), 10), | 82 | id: parseInt(localStorage.getItem(this.KEYS.ID), 10), |
82 | username: localStorage.getItem(this.KEYS.USERNAME), | 83 | username: localStorage.getItem(this.KEYS.USERNAME), |
83 | email: localStorage.getItem(this.KEYS.EMAIL), | 84 | email: localStorage.getItem(this.KEYS.EMAIL), |
84 | role: localStorage.getItem(this.KEYS.ROLE) as UserRole, | 85 | role: parseInt(localStorage.getItem(this.KEYS.ROLE), 10) as UserRole, |
85 | displayNSFW: localStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true' | 86 | displayNSFW: localStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true' |
86 | }, | 87 | }, |
87 | Tokens.load() | 88 | Tokens.load() |
@@ -122,11 +123,15 @@ export class AuthUser extends User { | |||
122 | this.tokens.refreshToken = refreshToken | 123 | this.tokens.refreshToken = refreshToken |
123 | } | 124 | } |
124 | 125 | ||
126 | hasRight(right: UserRight) { | ||
127 | return hasUserRight(this.role, right) | ||
128 | } | ||
129 | |||
125 | save () { | 130 | save () { |
126 | localStorage.setItem(AuthUser.KEYS.ID, this.id.toString()) | 131 | localStorage.setItem(AuthUser.KEYS.ID, this.id.toString()) |
127 | localStorage.setItem(AuthUser.KEYS.USERNAME, this.username) | 132 | localStorage.setItem(AuthUser.KEYS.USERNAME, this.username) |
128 | localStorage.setItem(AuthUser.KEYS.EMAIL, this.email) | 133 | localStorage.setItem(AuthUser.KEYS.EMAIL, this.email) |
129 | localStorage.setItem(AuthUser.KEYS.ROLE, this.role) | 134 | localStorage.setItem(AuthUser.KEYS.ROLE, this.role.toString()) |
130 | localStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW)) | 135 | localStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW)) |
131 | this.tokens.save() | 136 | this.tokens.save() |
132 | } | 137 | } |
diff --git a/client/src/app/core/auth/auth.service.ts b/client/src/app/core/auth/auth.service.ts index 9ac9ba7bb..df6e5135b 100644 --- a/client/src/app/core/auth/auth.service.ts +++ b/client/src/app/core/auth/auth.service.ts | |||
@@ -21,7 +21,7 @@ import { | |||
21 | // Do not use the barrel (dependency loop) | 21 | // Do not use the barrel (dependency loop) |
22 | import { RestExtractor } from '../../shared/rest' | 22 | import { RestExtractor } from '../../shared/rest' |
23 | import { UserLogin } from '../../../../../shared/models/users/user-login.model' | 23 | import { UserLogin } from '../../../../../shared/models/users/user-login.model' |
24 | import { User, UserConstructorHash } from '../../shared/users/user.model' | 24 | import { UserConstructorHash } from '../../shared/users/user.model' |
25 | 25 | ||
26 | interface UserLoginWithUsername extends UserLogin { | 26 | interface UserLoginWithUsername extends UserLogin { |
27 | access_token: string | 27 | access_token: string |
@@ -126,12 +126,6 @@ export class AuthService { | |||
126 | return this.user | 126 | return this.user |
127 | } | 127 | } |
128 | 128 | ||
129 | isAdmin () { | ||
130 | if (this.user === null) return false | ||
131 | |||
132 | return this.user.isAdmin() | ||
133 | } | ||
134 | |||
135 | isLoggedIn () { | 129 | isLoggedIn () { |
136 | return !!this.getAccessToken() | 130 | return !!this.getAccessToken() |
137 | } | 131 | } |
diff --git a/client/src/app/core/auth/index.ts b/client/src/app/core/auth/index.ts index a81f2c002..bc7bfec0e 100644 --- a/client/src/app/core/auth/index.ts +++ b/client/src/app/core/auth/index.ts | |||
@@ -1,4 +1,4 @@ | |||
1 | export * from './auth-status.model' | 1 | export * from './auth-status.model' |
2 | export * from './auth-user.model' | 2 | export * from './auth-user.model' |
3 | export * from './auth.service' | 3 | export * from './auth.service' |
4 | export * from './login-guard.service' | 4 | export * from '../routing/login-guard.service' |
diff --git a/client/src/app/core/auth/login-guard.service.ts b/client/src/app/core/auth/login-guard.service.ts deleted file mode 100644 index c09e8fe97..000000000 --- a/client/src/app/core/auth/login-guard.service.ts +++ /dev/null | |||
@@ -1,30 +0,0 @@ | |||
1 | import { Injectable } from '@angular/core' | ||
2 | import { | ||
3 | ActivatedRouteSnapshot, | ||
4 | CanActivateChild, | ||
5 | RouterStateSnapshot, | ||
6 | CanActivate, | ||
7 | Router | ||
8 | } from '@angular/router' | ||
9 | |||
10 | import { AuthService } from './auth.service' | ||
11 | |||
12 | @Injectable() | ||
13 | export class LoginGuard implements CanActivate, CanActivateChild { | ||
14 | |||
15 | constructor ( | ||
16 | private router: Router, | ||
17 | private auth: AuthService | ||
18 | ) {} | ||
19 | |||
20 | canActivate (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { | ||
21 | if (this.auth.isLoggedIn() === true) return true | ||
22 | |||
23 | this.router.navigate([ '/login' ]) | ||
24 | return false | ||
25 | } | ||
26 | |||
27 | canActivateChild (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { | ||
28 | return this.canActivate(route, state) | ||
29 | } | ||
30 | } | ||