This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| axios.interceptors.response.use(response => { | |
| return response; | |
| }, err => { | |
| return new Promise((resolve, reject) => { | |
| const originalReq = err.config; | |
| if ( err.response.status === 401 && err.config && !err.config.__isRetryRequest ) | |
| { | |
| originalReq._retry = true; | |
| let res = fetch('http://localhost:8080/api/v1/auth/refresh', { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import axios from 'axios'; | |
| export default class authService { | |
| init = () => { | |
| this.setInterceptors(); | |
| }; | |
| setInterceptors = () => { | |
| axios.defaults.headers.common['Token'] = localStorage.getItem("token"); | |
| axios.defaults.headers.common['Device'] = "device"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func CreateToken(email, ua, password string) (string, error) { | |
| //Creating empty slice | |
| r := make([]byte, 24) | |
| //Push random data to slice | |
| rand.Read(r) | |
| //Generate secret code for jwt | |
| secret := sha3.New512().Sum(append([]byte(email + ua + password), r...)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> | |
| { | |
| if (//check if token is expired) { | |
| if (queue.length == 0) { | |
| //Reload token here | |
| //Start all req from queue | |
| }else { | |
| //Put req to the queue | |
| } | |
| }else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Injectable() | |
| export class AuthInterceptor implements HttpInterceptor | |
| { | |
| constructor(private _router: Router) { } | |
| intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> | |
| { | |
| let token = localStorage.getItem('api-token'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Genrate params for token refreshing | |
| let params = { | |
| token: token, | |
| refreshToken: localStorange("refreshToken"); | |
| }; | |
| return this.http.post('localhost:8080/auth/refresh', params).flatMap( | |
| (data: any) => { | |
| //If reload successful update tokens | |
| if (data.status == 200) { | |
| //Update tokens |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Injectable() | |
| export class AuthInterceptor implements HttpInterceptor | |
| { | |
| constructor(private _router: Router) { } | |
| intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> | |
| { | |
| let token = localStorage.getItem('api-token'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| return next.handle(req).catch(err => { | |
| console.log(err); | |
| if (err.status === 401) { | |
| if (err.error.message == "Token is exp") { | |
| //TODO: Token refreshing | |
| }else { | |
| //Logout from account or do some other stuff | |
| } | |
| } | |
| return Observable.throw(err); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| providers : [ | |
| ... | |
| { | |
| provide: HTTP_INTERCEPTORS, | |
| useClass: AuthInterceptor, | |
| multi: true | |
| } | |
| ... | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import {Injectable} from "@angular/core"; | |
| import {HttpEvent, HttpHandler, HttpRequest, HttpClient} from "@angular/common/http"; | |
| import {HttpInterceptor} from "@angular/common/http"; | |
| import { Observable } from 'rxjs/Observable'; | |
| @Injectable() | |
| export class AuthInterceptor implements HttpInterceptor | |
| { | |
| constructor(public http: HttpClient) { } |