feat: add user login expire time

This commit is contained in:
xion 2025-02-16 03:05:21 +08:00
parent 17322033ad
commit a52ae9ea3a
2 changed files with 12 additions and 9 deletions

View File

@ -36,9 +36,12 @@ export class User extends Model {
* @param uid * @param uid
* @returns * @returns
*/ */
async createToken(uid?: string) { async createToken(uid?: string, loginType?: 'default' | 'plugin') {
const { id, username, type } = this; const { id, username, type } = this;
const expireTime = 60 * 60 * 24 * 7; // 7 days let expireTime = 60 * 60 * 24 * 7; // 7 days
if (loginType === 'plugin') {
expireTime = 60 * 60 * 24 * 30; // 30 days
}
const now = new Date().getTime(); const now = new Date().getTime();
const token = await createToken({ id, username, uid, type }, config.tokenSecret); const token = await createToken({ id, username, uid, type }, config.tokenSecret);
return { token, expireTime: now + expireTime }; return { token, expireTime: now + expireTime };

View File

@ -27,7 +27,7 @@ app
key: 'login', key: 'login',
}) })
.define(async (ctx) => { .define(async (ctx) => {
const { username, email, password } = ctx.query; const { username, email, password, loginType = 'default' } = ctx.query;
if (!username && !email) { if (!username && !email) {
ctx.throw(400, 'username or email is required'); ctx.throw(400, 'username or email is required');
} }
@ -50,7 +50,7 @@ app
if (!user.checkPassword(password)) { if (!user.checkPassword(password)) {
ctx.throw(500, 'Password error'); ctx.throw(500, 'Password error');
} }
const token = await user.createToken(); const token = await user.createToken(null, loginType);
ctx.res.cookie('token', token.token, { ctx.res.cookie('token', token.token, {
maxAge: token.expireTime, maxAge: token.expireTime,
domain: 'xiongxiao.me', domain: 'xiongxiao.me',
@ -128,7 +128,7 @@ app
}) })
.define(async (ctx) => { .define(async (ctx) => {
const tokenUser = ctx.state.tokenUser; const tokenUser = ctx.state.tokenUser;
const { username, type = 'org' } = ctx.query.data || {}; const { username, type = 'org', loginType } = ctx.query.data || {};
if (!username && type === 'org') { if (!username && type === 'org') {
ctx.throw('username is required'); ctx.throw('username is required');
} }
@ -139,11 +139,11 @@ app
ctx.throw('user not found'); ctx.throw('user not found');
} }
if (user.type === 'user') { if (user.type === 'user') {
const token = await user.createToken(); const token = await user.createToken(null, loginType);
ctx.body = token; ctx.body = token;
return; return;
} else if (user.type === 'org' && tokenUser.uid) { } else if (user.type === 'org' && tokenUser.uid) {
const token = await user.createToken(tokenUser.uid); const token = await user.createToken(tokenUser.uid, loginType);
ctx.body = token; ctx.body = token;
return; return;
} }
@ -159,7 +159,7 @@ app
ctx.throw('Permission denied'); ctx.throw('Permission denied');
} }
if (type === 'user') { if (type === 'user') {
const token = await me.createToken(); const token = await me.createToken(null, loginType);
ctx.body = token; ctx.body = token;
return; return;
} }
@ -173,7 +173,7 @@ app
if (index === -1) { if (index === -1) {
ctx.throw('Permission denied'); ctx.throw('Permission denied');
} }
const token = await orgUser.createToken(me.id); const token = await orgUser.createToken(me.id, loginType);
ctx.body = token; ctx.body = token;
}) })
.addTo(app); .addTo(app);