feat: add user login expire time
This commit is contained in:
parent
17322033ad
commit
a52ae9ea3a
@ -36,9 +36,12 @@ export class User extends Model {
|
||||
* @param uid
|
||||
* @returns
|
||||
*/
|
||||
async createToken(uid?: string) {
|
||||
async createToken(uid?: string, loginType?: 'default' | 'plugin') {
|
||||
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 token = await createToken({ id, username, uid, type }, config.tokenSecret);
|
||||
return { token, expireTime: now + expireTime };
|
||||
|
@ -27,7 +27,7 @@ app
|
||||
key: 'login',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { username, email, password } = ctx.query;
|
||||
const { username, email, password, loginType = 'default' } = ctx.query;
|
||||
if (!username && !email) {
|
||||
ctx.throw(400, 'username or email is required');
|
||||
}
|
||||
@ -50,7 +50,7 @@ app
|
||||
if (!user.checkPassword(password)) {
|
||||
ctx.throw(500, 'Password error');
|
||||
}
|
||||
const token = await user.createToken();
|
||||
const token = await user.createToken(null, loginType);
|
||||
ctx.res.cookie('token', token.token, {
|
||||
maxAge: token.expireTime,
|
||||
domain: 'xiongxiao.me',
|
||||
@ -128,7 +128,7 @@ app
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { username, type = 'org' } = ctx.query.data || {};
|
||||
const { username, type = 'org', loginType } = ctx.query.data || {};
|
||||
if (!username && type === 'org') {
|
||||
ctx.throw('username is required');
|
||||
}
|
||||
@ -139,11 +139,11 @@ app
|
||||
ctx.throw('user not found');
|
||||
}
|
||||
if (user.type === 'user') {
|
||||
const token = await user.createToken();
|
||||
const token = await user.createToken(null, loginType);
|
||||
ctx.body = token;
|
||||
return;
|
||||
} 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;
|
||||
return;
|
||||
}
|
||||
@ -159,7 +159,7 @@ app
|
||||
ctx.throw('Permission denied');
|
||||
}
|
||||
if (type === 'user') {
|
||||
const token = await me.createToken();
|
||||
const token = await me.createToken(null, loginType);
|
||||
ctx.body = token;
|
||||
return;
|
||||
}
|
||||
@ -173,7 +173,7 @@ app
|
||||
if (index === -1) {
|
||||
ctx.throw('Permission denied');
|
||||
}
|
||||
const token = await orgUser.createToken(me.id);
|
||||
const token = await orgUser.createToken(me.id, loginType);
|
||||
ctx.body = token;
|
||||
})
|
||||
.addTo(app);
|
||||
|
Loading…
x
Reference in New Issue
Block a user