Skip to content

Commit

Permalink
API AuthenticateでDB接続エラーなどが発生するとログアウトさせられてしまうのを修正
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 committed Jul 17, 2021
1 parent 439b5d4 commit 130201b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/client/app/mios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export default class MiOS extends EventEmitter {
// When success
.then(res => {
// When failed to authenticate user
if (res.status !== 200 && res.status < 500) {
if (res.status >= 400 && res.status < 500) {
return this.signout();
}

Expand Down
18 changes: 11 additions & 7 deletions src/server/api/api-handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Koa from 'koa';

import { IEndpoint } from './endpoints';
import authenticate from './authenticate';
import authenticate, { AuthenticationError } from './authenticate';
import call from './call';
import { ApiError } from './error';

Expand Down Expand Up @@ -36,11 +36,15 @@ export default (endpoint: IEndpoint, ctx: Koa.Context) => new Promise((res) => {
}).catch((e: ApiError) => {
reply(e.httpStatusCode ? e.httpStatusCode : e.kind == 'client' ? 400 : 500, e);
});
}).catch(() => {
reply(403, new ApiError({
message: 'Authentication failed. Please ensure your token is correct.',
code: 'AUTHENTICATION_FAILED',
id: 'b0a7f5f8-dc2f-4171-b91f-de88ad238e14'
}));
}).catch(e => {
if (e instanceof AuthenticationError) {
reply(403, new ApiError({
message: 'Authentication failed. Please ensure your token is correct.',
code: 'AUTHENTICATION_FAILED',
id: 'b0a7f5f8-dc2f-4171-b91f-de88ad238e14'
}));
} else {
reply(500, new ApiError());
}
});
});
11 changes: 9 additions & 2 deletions src/server/api/authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import { User } from '../../models/entities/user';
import { App } from '../../models/entities/app';
import { Users, AccessTokens, Apps } from '../../models';

export class AuthenticationError extends Error {
constructor(message: string) {
super(message);
this.name = 'AuthenticationError';
}
}

export default async (token: string): Promise<[User | null | undefined, App | null | undefined]> => {
if (token == null) {
return [null, null];
Expand All @@ -14,7 +21,7 @@ export default async (token: string): Promise<[User | null | undefined, App | nu
.findOne({ token });

if (user == null) {
throw new Error('user not found');
throw new AuthenticationError('user not found');
}

return [user, null];
Expand All @@ -24,7 +31,7 @@ export default async (token: string): Promise<[User | null | undefined, App | nu
});

if (accessToken == null) {
throw new Error('invalid signature');
throw new AuthenticationError('invalid signature');
}

const app = await Apps
Expand Down

0 comments on commit 130201b

Please sign in to comment.