Answer the question
In order to leave comments, you need to log in
How to implement single sign-on?
I want to implement Single sign-on, but I don't quite understand how to do it. At the moment, when you start two clients and one server, you need to log in on both clients, how can you do a single sign-on so that you log in once on any of the clients and the rest no longer need authorization. The server and clients have the same domains (localhost), but the port is different for everyone.
token.interceptor.ts:
import { Injectable } from "@angular/core";
import { AuthService } from "../services/auth.service";
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpErrorResponse } from "@angular/common/http";
import { Observable, throwError } from "rxjs";
import { catchError } from "rxjs/operators";
import { Router } from "@angular/router";
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(private auth: AuthService, private router: Router){
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (this.auth.isAuthenticated()) {
req = req.clone({
setHeaders: {
Authorization: this.auth.getToken()
}
})
}
return next.handle(req).pipe(
catchError(
(error: HttpErrorResponse) => this.handleAuthError(error)
)
)
}
private handleAuthError(error: HttpErrorResponse): Observable<any> {
if (error.status === 401) {
this.router.navigate(['/login']), {
queryParams: {
sessionFailed: true
}
}
}
return throwError(error)
}
}
import { Component, OnInit } from '@angular/core';
import { AuthService } from './shared/services/auth.service';
@Component({
selector: 'app-root',
template: '<router-outlet></router-outlet>'
})
export class AppComponent implements OnInit {
constructor(private auth: AuthService) {}
ngOnInit() {
const potentialToken = localStorage.getItem('auth-token')
if (potentialToken !== null) {
this.auth.setToken(potentialToken)
}
}
}
import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { Observable } from "rxjs";
import { tap } from "rxjs/operators";
import { User } from "../interfaces";
@Injectable({
providedIn: 'root'
})
export class AuthService {
private token = null;
constructor(private http: HttpClient) {
}
login(user: User): Observable<{token: string}> {
return this.http.post<{token: string}>('/api/auth/login', user)
.pipe(
tap(
({token}) => {
localStorage.setItem('auth-token', token)
this.setToken(token)
}
)
)
}
setToken(token: string) {
this.token = token
}
getToken(): string {
return this.token
}
isAuthenticated(): boolean {
return !!this.token
}
logout() {
this.setToken(null)
localStorage.clear()
}
}
Answer the question
In order to leave comments, you need to log in
Read about openID and JWT. If you want to make an Identity Server, then please do not torture yourself and use ready-made solutions. At least the notorious Auth0
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question