Fix redirect to homepage after token refresh

This commit is contained in:
Sipke Schoorstra 2023-01-25 12:34:07 +01:00
parent fe845e65e7
commit 967b59d32d
5 changed files with 20 additions and 17 deletions

View file

@ -2,4 +2,5 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=abcdefghijklmnopqrstuvwxyz/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Deconstructor/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Postgre/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=richtext/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Telnyx/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View file

@ -1,7 +1,6 @@
import {Component, h, State, Event, EventEmitter} from "@stencil/core";
import {LoginApi} from "../services";
import {Container} from "typedi";
import jwt_decode from "jwt-decode";
import {AuthContext} from "../../../services";
import {SignedInArgs} from "../models";
@ -43,7 +42,7 @@ export class LoginPage {
const accessToken = loginResponse.accessToken;
const refreshToken = loginResponse.refreshToken;
const authContext = Container.get(AuthContext);
await authContext.signinTokens(accessToken, refreshToken, rememberMe);
await authContext.signin(accessToken, refreshToken, rememberMe);
}
private renderError = () => {

View file

@ -6,7 +6,6 @@ import {Container, Service} from "typedi";
import {StudioService, AuthContext, EventBus, ElsaClient, ElsaClientProvider} from "../../services";
import descriptorsStore from '../../data/descriptors-store';
import {SignedInArgs} from "./models";
import {AxiosInstance} from "axios";
import {LoginApi} from "./services";
@Service()
@ -64,14 +63,16 @@ export class LoginPlugin implements Plugin {
},
async onResponseError(error) {
if (error.response.status !== 401 || error.response.config.hasRetriedRequest)
if (error.response.status !== 401 || error.response.config.hasRetriedRequest)
return;
const authContext = Container.get(AuthContext);
const loginResponse = await loginApi.refreshAccessToken(authContext.getRefreshToken());
if (loginResponse.isAuthenticated) {
await authContext.signinTokens(loginResponse.accessToken, loginResponse.refreshToken, true);
await authContext.updateTokens(loginResponse.accessToken, loginResponse.refreshToken, true);
const t = await service.http({
...error.config,
@ -85,7 +86,7 @@ export class LoginPlugin implements Plugin {
return t;
}
else {
authContext.signOut();
await authContext.signOut();
}
}
});

View file

@ -1,9 +1,8 @@
import 'reflect-metadata';
import {Container, Service} from "typedi";
import {ElsaClientProvider, EventBus, ServerSettings} from "../../services";
import {Service} from "typedi";
import {ElsaClientProvider, ServerSettings} from "../../services";
import {LoginResponse} from "./models";
import axios, {AxiosError, AxiosRequestConfig} from "axios";
import {EventTypes} from "../../models";
import axios, {AxiosRequestConfig} from "axios";
@Service()
export class LoginApi {
@ -32,4 +31,4 @@ export class LoginApi {
.then(response => response.data)
.catch(error => error.response)
}
}
}

View file

@ -29,14 +29,19 @@ export class AuthContext {
}
}
async signinTokens(accessToken: string, refreshToken: string, createPersistentCookie: boolean){
async signin(accessToken: string, refreshToken: string, createPersistentCookie: boolean){
await this.updateTokens(accessToken, refreshToken, createPersistentCookie);
await this.eventBus.emit(EventTypes.Auth.SignedIn)
}
async updateTokens(accessToken: string, refreshToken: string, createPersistentCookie: boolean){
const claims = jwt_decode<any>(accessToken);
const permissions = claims.permissions || [];
const name = claims.name || '';
await this.signIn(name, permissions, accessToken, refreshToken, createPersistentCookie);
await this.updateSession(name, permissions, accessToken, refreshToken, createPersistentCookie);
}
async signIn(name: string, permissions: Array<string>, accessToken: string, refreshToken: string, createPersistentCookie: boolean) {
updateSession(name: string, permissions: Array<string>, accessToken: string, refreshToken: string, createPersistentCookie: boolean) {
authStore.name = name;
authStore.permissions = permissions;
authStore.signedIn = true;
@ -49,8 +54,6 @@ export class AuthContext {
if (createPersistentCookie) {
cookies.set("dashboard-session", data);
}
await this.eventBus.emit(EventTypes.Auth.SignedIn)
}
async signOut() {