Skip to content

Commit

Permalink
Added logout on token expiry
Browse files Browse the repository at this point in the history
  • Loading branch information
HeeManSu committed Dec 15, 2024
1 parent 09c65fb commit 17b4ad1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { error, info, warn } from './cli/messages';
import { loginSelection } from './cli/selection';
import { Config, save } from './config';
import { ErrorCode } from './deploy';
import { logout } from './logout';
import { forever } from './utils';

const authToken = async (config: Config): Promise<string> => {
Expand Down Expand Up @@ -54,8 +55,15 @@ const authToken = async (config: Config): Promise<string> => {
}

if (expiresIn(token) < config.renewTime) {
// Token expires in < renewTime
token = await api.refresh();
try {
// Attempt to refresh token
token = await api.refresh();
} catch (err) {
// If refresh fails, force logout
await logout();
info('Session expired. Please login again with email/password.');
process.exit(1);
}
}

return token;
Expand Down
11 changes: 9 additions & 2 deletions src/cli/validateToken.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { API as APIInterface } from '@metacall/protocol/protocol';
import { unlink } from 'fs/promises';
import { configFilePath, save } from '../config';
import { logout } from '../logout';
import { exists } from '../utils';
import args from './args';
import { error, info } from './messages';
Expand All @@ -9,8 +10,14 @@ const handleValidateToken = async (api: APIInterface): Promise<void> => {
const validToken = await api.validate();

if (!validToken) {
const token = await api.refresh();
await save({ token });
try {
const token = await api.refresh();
await save({ token });
} catch (err) {
await logout();
info('Token expired. Please login again.');
process.exit(1);
}
}
};

Expand Down

0 comments on commit 17b4ad1

Please sign in to comment.