update: google oauth2.0 with hd=email@forth.co.th only now functional
This commit is contained in:
parent
984707c7bf
commit
36c71eda38
31 changed files with 580 additions and 317 deletions
|
|
@ -3,13 +3,14 @@ import { HttpClient } from '@angular/common/http';
|
|||
import {
|
||||
BehaviorSubject,
|
||||
Observable,
|
||||
concat,
|
||||
distinctUntilChanged,
|
||||
map,
|
||||
tap,
|
||||
} from 'rxjs';
|
||||
import { User } from '../models/user.model';
|
||||
import { Router } from '@angular/router';
|
||||
import { JwtService } from './jwt.service';
|
||||
import { environment } from 'src/environments/environment';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class UserService {
|
||||
|
|
@ -18,64 +19,45 @@ export class UserService {
|
|||
.asObservable()
|
||||
.pipe(distinctUntilChanged());
|
||||
|
||||
public isAuthenticated = this.currentUser.pipe(
|
||||
map((user) => {
|
||||
return user !== null;
|
||||
})
|
||||
);
|
||||
public isAuthenticated = this.currentUser.pipe(map((user) => !!user));
|
||||
|
||||
constructor(
|
||||
private readonly http: HttpClient,
|
||||
private readonly router: Router,
|
||||
private readonly jwtService: JwtService
|
||||
private readonly router: Router
|
||||
) {}
|
||||
|
||||
login(credentials: {
|
||||
email: string;
|
||||
password: string;
|
||||
}): Observable<{ user: User }> {
|
||||
return this.http
|
||||
.post<{ user: User }>('/api/users/login', credentials)
|
||||
.pipe(tap(({ user }) => this.setAuth(user)));
|
||||
}
|
||||
|
||||
register(credentials: {
|
||||
username: string;
|
||||
email: string;
|
||||
password: string;
|
||||
}): Observable<{ user: User }> {
|
||||
return this.http
|
||||
.post<{ user: User }>('/api/users', { user: credentials })
|
||||
.pipe(tap(({ user }) => this.setAuth(user)));
|
||||
}
|
||||
|
||||
logout(): void {
|
||||
this.purgeAuth();
|
||||
void this.router.navigate(['/login']);
|
||||
// post to api /revoke with cookie
|
||||
this.http
|
||||
.get(environment.api + '/auth/revoke', {
|
||||
withCredentials: true,
|
||||
})
|
||||
.subscribe({
|
||||
complete: () => this.router.navigateByUrl('/login'),
|
||||
});
|
||||
}
|
||||
|
||||
getCurrentUser(): Observable<{ user: User }> {
|
||||
return this.http.get<{ user: User }>('/api/user').pipe(
|
||||
tap({
|
||||
next: ({ user }) => this.setAuth(user),
|
||||
error: () => this.purgeAuth(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
update(user: Partial<User>): Observable<{ user: User }> {
|
||||
return this.http
|
||||
.put<{ user: User }>('/api/user', { user })
|
||||
.pipe(tap(({ user }) => this.currnetUserSubject.next(user)));
|
||||
.get<{ user: User }>(environment.api + '/auth/user', {
|
||||
withCredentials: true,
|
||||
})
|
||||
.pipe(
|
||||
tap({
|
||||
next: ({ user }) => {
|
||||
this.setAuth(user);
|
||||
},
|
||||
error: () => this.purgeAuth(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
setAuth(user: User): void {
|
||||
this.jwtService.saveToken(user.token);
|
||||
void this.currnetUserSubject.next(user);
|
||||
}
|
||||
|
||||
purgeAuth(): void {
|
||||
this.jwtService.destroyToken();
|
||||
this.currnetUserSubject.next(null);
|
||||
void this.currnetUserSubject.next(null);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue