Reviewed-on: sh-edraft.de/kd_discord_bot#228 Reviewed-by: edraft-dev <dev.sven.heidemann@sh-edraft.de> Closes #210
This commit is contained in:
@@ -19,7 +19,7 @@ import { DiscordAuthURL } from "../../models/auth/discord-auth-url.dto";
|
||||
import { OAuthDTO } from "../../models/auth/oauth.dto";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
providedIn: "root"
|
||||
})
|
||||
export class AuthService {
|
||||
|
||||
@@ -42,14 +42,15 @@ export class AuthService {
|
||||
getAllUsers(): Observable<Array<AuthUserDTO>> {
|
||||
return this.http.get<Array<AuthUserDTO>>(`${this.appsettings.getApiURL()}/api/auth/users`, {
|
||||
headers: new HttpHeaders({
|
||||
'Content-Type': 'application/json'
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
getFilteredUsers(selectCriterions: AuthUserSelectCriterion): Observable<GetFilteredAuthUsersResultDTO> {
|
||||
return this.http.post<GetFilteredAuthUsersResultDTO>(`${this.appsettings.getApiURL()}/api/auth/users/get/filtered`, selectCriterions, {
|
||||
headers: new HttpHeaders({
|
||||
'Content-Type': 'application/json'
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -57,7 +58,7 @@ export class AuthService {
|
||||
getUserByEMail(email: string): Observable<AuthUserDTO> {
|
||||
return this.http.get<AuthUserDTO>(`${this.appsettings.getApiURL()}/api/auth/users/get/${email}`, {
|
||||
headers: new HttpHeaders({
|
||||
'Content-Type': 'application/json'
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -65,7 +66,7 @@ export class AuthService {
|
||||
findUserByEMail(email: string): Observable<AuthUserDTO> {
|
||||
return this.http.get<AuthUserDTO>(`${this.appsettings.getApiURL()}/api/auth/users/find/${email}`, {
|
||||
headers: new HttpHeaders({
|
||||
'Content-Type': 'application/json'
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -74,7 +75,7 @@ export class AuthService {
|
||||
register(user: AuthUserDTO): Observable<unknown> {
|
||||
return this.http.post<TokenDTO>(`${this.appsettings.getApiURL()}/api/auth/register`, user, {
|
||||
headers: new HttpHeaders({
|
||||
'Content-Type': 'application/json'
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -82,7 +83,7 @@ export class AuthService {
|
||||
confirmEMail(id: string): Observable<boolean> {
|
||||
return this.http.post<boolean>(`${this.appsettings.getApiURL()}/api/auth/register-by-id/${id}`, {
|
||||
headers: new HttpHeaders({
|
||||
'Content-Type': 'application/json'
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -90,7 +91,7 @@ export class AuthService {
|
||||
login(user: AuthUserDTO): Observable<TokenDTO> {
|
||||
return this.http.post<TokenDTO>(`${this.appsettings.getApiURL()}/api/auth/login`, user, {
|
||||
headers: new HttpHeaders({
|
||||
'Content-Type': 'application/json'
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -98,7 +99,7 @@ export class AuthService {
|
||||
verifyLogin(): Observable<boolean> {
|
||||
return this.http.get<boolean>(`${this.appsettings.getApiURL()}/api/auth/verify-login`, {
|
||||
headers: new HttpHeaders({
|
||||
'Content-Type': 'application/json'
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -106,7 +107,7 @@ export class AuthService {
|
||||
forgotPassword(email: string): Observable<unknown> {
|
||||
return this.http.post(`${this.appsettings.getApiURL()}/api/auth/forgot-password/${email}`, {
|
||||
headers: new HttpHeaders({
|
||||
'Content-Type': 'application/json'
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -114,7 +115,7 @@ export class AuthService {
|
||||
getEMailFromforgotPasswordId(id: string): Observable<EMailStringDTO> {
|
||||
return this.http.post<EMailStringDTO>(`${this.appsettings.getApiURL()}/api/auth/confirm-forgot-password/${id}`, {
|
||||
headers: new HttpHeaders({
|
||||
'Content-Type': 'application/json'
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -122,7 +123,7 @@ export class AuthService {
|
||||
resetPassword(resetPasswordDTO: ResetPasswordDTO): Observable<unknown> {
|
||||
return this.http.post(`${this.appsettings.getApiURL()}/api/auth/reset-password`, resetPasswordDTO, {
|
||||
headers: new HttpHeaders({
|
||||
'Content-Type': 'application/json'
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -130,7 +131,7 @@ export class AuthService {
|
||||
updateUser(updateUserDTO: UpdateUserDTO): Observable<unknown> {
|
||||
return this.http.post(`${this.appsettings.getApiURL()}/api/auth/update-user`, updateUserDTO, {
|
||||
headers: new HttpHeaders({
|
||||
'Content-Type': 'application/json'
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -138,7 +139,7 @@ export class AuthService {
|
||||
updateUserAsAdmin(updateUserDTO: AdminUpdateUserDTO): Observable<unknown> {
|
||||
return this.http.post(`${this.appsettings.getApiURL()}/api/auth/update-user-as-admin`, updateUserDTO, {
|
||||
headers: new HttpHeaders({
|
||||
'Content-Type': 'application/json'
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -146,7 +147,7 @@ export class AuthService {
|
||||
refresh(token: TokenDTO): Observable<TokenDTO> {
|
||||
return this.http.post<TokenDTO>(`${this.appsettings.getApiURL()}/api/auth/refresh`, token, {
|
||||
headers: new HttpHeaders({
|
||||
'Content-Type': 'application/json'
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -154,7 +155,7 @@ export class AuthService {
|
||||
deleteUserByMail(mail: string) {
|
||||
return this.http.post(`${this.appsettings.getApiURL()}/api/auth/delete-user-by-mail/${mail}`, {
|
||||
headers: new HttpHeaders({
|
||||
'Content-Type': 'application/json'
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -163,7 +164,7 @@ export class AuthService {
|
||||
getDiscordAuthURL() {
|
||||
return this.http.get<DiscordAuthURL>(`${this.appsettings.getApiURL()}/api/auth/discord/get-url`, {
|
||||
headers: new HttpHeaders({
|
||||
'Content-Type': 'application/json'
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -171,7 +172,7 @@ export class AuthService {
|
||||
discordLogin(code: string, state: string): Observable<TokenDTO> {
|
||||
return this.http.get<TokenDTO>(`${this.appsettings.getApiURL()}/api/auth/discord/login?code=${code}&state=${state}`, {
|
||||
headers: new HttpHeaders({
|
||||
'Content-Type': 'application/json'
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -180,7 +181,7 @@ export class AuthService {
|
||||
discordRegister(oAuthDTO: OAuthDTO) {
|
||||
return this.http.post(`${this.appsettings.getApiURL()}/api/auth/discord/register`, oAuthDTO, {
|
||||
headers: new HttpHeaders({
|
||||
'Content-Type': 'application/json'
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -195,21 +196,25 @@ export class AuthService {
|
||||
|
||||
/* utils */
|
||||
saveToken(token: TokenDTO): void {
|
||||
localStorage.setItem('jwt', token.token);
|
||||
localStorage.setItem('rjwt', token.refreshToken);
|
||||
if (this.router.url.startsWith('/auth')) {
|
||||
this.router.navigate(['/dashboard']);
|
||||
localStorage.setItem("jwt", token.token);
|
||||
localStorage.setItem("rjwt", token.refreshToken);
|
||||
if (this.router.url.startsWith("/auth")) {
|
||||
this.router.navigate(["/dashboard"]);
|
||||
}
|
||||
}
|
||||
|
||||
getToken(): TokenDTO {
|
||||
return {
|
||||
token: localStorage.getItem('jwt') ?? '',
|
||||
refreshToken: localStorage.getItem('rjwt') ?? ''
|
||||
token: localStorage.getItem("jwt") ?? "",
|
||||
refreshToken: localStorage.getItem("rjwt") ?? ""
|
||||
};
|
||||
}
|
||||
|
||||
getDecodedToken(): { [key: string]: any } | null{
|
||||
getDecodedToken(token: TokenDTO | undefined = undefined): { [key: string]: any } | null {
|
||||
if (token) {
|
||||
return this.jwtHelper.decodeToken(token.token);
|
||||
}
|
||||
|
||||
return this.jwtHelper.decodeToken(this.getToken().token);
|
||||
}
|
||||
|
||||
@@ -219,26 +224,26 @@ export class AuthService {
|
||||
if (token && token.token && token.refreshToken) {
|
||||
return this.http.post<TokenDTO>(`${this.appsettings.getApiURL()}/api/auth/revoke`, token, {
|
||||
headers: new HttpHeaders({
|
||||
'Content-Type': 'application/json'
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
}).pipe(catchError((error: any) => {
|
||||
error.error = null;
|
||||
this.isLoggedIn$.next(false);
|
||||
localStorage.removeItem('rjwt');
|
||||
this.router.navigate(['/auth/login']);
|
||||
localStorage.removeItem("rjwt");
|
||||
this.router.navigate(["/auth/login"]);
|
||||
throw error;
|
||||
})).subscribe(() => {
|
||||
this.isLoggedIn$.next(false);
|
||||
localStorage.removeItem('jwt');
|
||||
localStorage.removeItem('rjwt');
|
||||
this.router.navigate(['/auth/login']);
|
||||
localStorage.removeItem("jwt");
|
||||
localStorage.removeItem("rjwt");
|
||||
this.router.navigate(["/auth/login"]);
|
||||
});
|
||||
}
|
||||
this.isLoggedIn$.next(false);
|
||||
localStorage.removeItem('rjwt');
|
||||
this.router.navigate(['/auth/login']);
|
||||
localStorage.removeItem("rjwt");
|
||||
this.router.navigate(["/auth/login"]);
|
||||
|
||||
return null
|
||||
return null;
|
||||
}
|
||||
|
||||
async getLoggedInUser(): Promise<AuthUserDTO | null> {
|
||||
@@ -295,13 +300,13 @@ export class AuthService {
|
||||
}
|
||||
const token = this.getDecodedToken();
|
||||
if (!token) return false;
|
||||
return AuthRoles[token['role']] === AuthRoles[role];
|
||||
return AuthRoles[token["role"]] === AuthRoles[role];
|
||||
}
|
||||
|
||||
getEMailFromDecodedToken(token: { [key: string]: any } | null): string | null {
|
||||
if (!token) {
|
||||
return null;
|
||||
}
|
||||
return token['email'];
|
||||
return token["email"];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user