Improve logout
This commit is contained in:
parent
55fdeaf086
commit
dc8272b15f
5 changed files with 57 additions and 21 deletions
|
@ -1,5 +1,5 @@
|
||||||
import { Router, CanActivateFn } from '@angular/router';
|
import { Router, CanActivateFn } from '@angular/router';
|
||||||
import { AuthService } from './login/auth.service'
|
import { AuthService } from './login/auth.service';
|
||||||
import { inject } from '@angular/core';
|
import { inject } from '@angular/core';
|
||||||
|
|
||||||
export const AuthGuard: CanActivateFn = (route, state) => {
|
export const AuthGuard: CanActivateFn = (route, state) => {
|
||||||
|
@ -7,16 +7,14 @@ export const AuthGuard: CanActivateFn = (route, state) => {
|
||||||
const authSvc: AuthService = inject(AuthService);
|
const authSvc: AuthService = inject(AuthService);
|
||||||
if (sessionStorage.getItem('jwt') == null) {
|
if (sessionStorage.getItem('jwt') == null) {
|
||||||
let success = false;
|
let success = false;
|
||||||
authSvc.refresh()
|
authSvc.refresh().subscribe((event) => {
|
||||||
.subscribe((event) => {
|
if (event?.status == 200) {
|
||||||
if (event?.status == 200) {
|
success = true;
|
||||||
success = true;
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
router.navigate(['/login']);
|
router.navigate(['/login']);
|
||||||
return success;
|
return success;
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -41,24 +41,33 @@ export class AuthService {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
refresh(): Observable<HttpResponse<Jwt>> {
|
logout() {
|
||||||
return this.http.get<Jwt>('/api/refresh', { withCredentials: true, observe: 'response' }).pipe(
|
this.http
|
||||||
tap((event) => {
|
.get('/api/logout', { withCredentials: true, observe: 'response' })
|
||||||
|
.subscribe((event) => {
|
||||||
if (event.status == 200) {
|
if (event.status == 200) {
|
||||||
sessionStorage.setItem('jwt', event.body?.jwt.toString() ?? '');
|
this.loggedIn = false;
|
||||||
this.loggedIn = true;
|
|
||||||
}
|
}
|
||||||
}),
|
});
|
||||||
);
|
sessionStorage.removeItem('jwt');
|
||||||
|
this.loggedIn = false;
|
||||||
|
this._router.navigateByUrl('/login');
|
||||||
|
}
|
||||||
|
|
||||||
|
refresh(): Observable<HttpResponse<Jwt>> {
|
||||||
|
return this.http
|
||||||
|
.get<Jwt>('/api/refresh', { withCredentials: true, observe: 'response' })
|
||||||
|
.pipe(
|
||||||
|
tap((event) => {
|
||||||
|
if (event.status == 200) {
|
||||||
|
sessionStorage.setItem('jwt', event.body?.jwt.toString() ?? '');
|
||||||
|
this.loggedIn = true;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
getToken(): string | null {
|
getToken(): string | null {
|
||||||
return sessionStorage.getItem('jwt');
|
return sessionStorage.getItem('jwt');
|
||||||
}
|
}
|
||||||
|
|
||||||
logout() {
|
|
||||||
sessionStorage.removeItem('jwt');
|
|
||||||
this.loggedIn = false;
|
|
||||||
this._router.navigateByUrl('/login');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,5 +5,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const Prefix = "admin/dist/admin/browser"
|
const Prefix = "admin/dist/admin/browser"
|
||||||
|
|
||||||
//go:embed admin/dist/admin/browser
|
//go:embed admin/dist/admin/browser
|
||||||
var Client embed.FS
|
var Client embed.FS
|
||||||
|
|
|
@ -77,6 +77,7 @@ func (a *Auth) PublicRoutes(r chi.Router) {
|
||||||
|
|
||||||
func (a *Auth) PrivateRoutes(r chi.Router) {
|
func (a *Auth) PrivateRoutes(r chi.Router) {
|
||||||
r.Get("/api/refresh", a.routeRefresh)
|
r.Get("/api/refresh", a.routeRefresh)
|
||||||
|
r.Get("/api/logout", a.routeLogout)
|
||||||
}
|
}
|
||||||
|
|
||||||
//go:embed login.html
|
//go:embed login.html
|
||||||
|
|
|
@ -237,3 +237,30 @@ func (a *Auth) routeAuth(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
render.JSON(w, r, &jr)
|
render.JSON(w, r, &jr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *Auth) routeLogout(w http.ResponseWriter, r *http.Request) {
|
||||||
|
cookie := &http.Cookie{
|
||||||
|
Name: "jwt",
|
||||||
|
Value: "",
|
||||||
|
HttpOnly: true,
|
||||||
|
Secure: true,
|
||||||
|
Expires: time.Time{},
|
||||||
|
}
|
||||||
|
|
||||||
|
if a.allowInsecureCookie(r) {
|
||||||
|
cookie.Secure = false
|
||||||
|
cookie.SameSite = http.SameSiteLaxMode
|
||||||
|
} else {
|
||||||
|
cookie.Domain = a.cfg.Domain
|
||||||
|
}
|
||||||
|
|
||||||
|
http.SetCookie(w, cookie)
|
||||||
|
|
||||||
|
jr := struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
}{
|
||||||
|
Message: "logged out",
|
||||||
|
}
|
||||||
|
|
||||||
|
render.JSON(w, r, &jr)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue