fix: invalid token redirect and error handling

This commit is contained in:
apotdevin 2022-02-01 22:01:35 +01:00
parent ca1c7292a4
commit 194dafb940
No known key found for this signature in database
GPG key ID: 4403F1DFBE779457
2 changed files with 26 additions and 7 deletions

View file

@ -1,5 +1,11 @@
import { useGetNodeInfoQuery } from '../../src/graphql/queries/__generated__/getNodeInfo.generated';
import { getVersion } from '../../src/utils/version';
import getConfig from 'next/config';
import { useEffect } from 'react';
import { toast } from 'react-toastify';
const { publicRuntimeConfig } = getConfig();
const { logoutUrl, basePath } = publicRuntimeConfig;
type StatusState = {
alias: string;
@ -34,6 +40,12 @@ const initialState: StatusState = {
export const useNodeInfo = (): StatusState => {
const { data, loading, error } = useGetNodeInfoQuery();
useEffect(() => {
if (!error) return;
toast.error(`Unable to connect to node`);
window.location.href = logoutUrl || `${basePath}/login`;
}, [error]);
if (!data?.getNodeInfo || loading || error) {
return initialState;
}

View file

@ -1,23 +1,30 @@
import { Injectable } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger } from 'winston';
import { AccountsService } from '../accounts/accounts.service';
@Injectable()
export class AuthenticationService {
constructor(
private readonly jwtService: JwtService,
private accountsService: AccountsService
private accountsService: AccountsService,
@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger
) {}
public async getUserFromAuthToken(token: string) {
const payload = this.jwtService.verify(token);
try {
const payload = this.jwtService.verify(token);
if (payload.sub) {
const account = this.accountsService.getAccount(payload.sub);
if (payload.sub) {
const account = this.accountsService.getAccount(payload.sub);
if (account) {
return payload.sub;
if (account) {
return payload.sub;
}
}
} catch (error) {
this.logger.error('Invalid token for authentication');
}
}
}