添加通知api

This commit is contained in:
whyour 2021-09-17 18:06:14 +08:00
parent 3ddaf41e27
commit aed08b0f59
4 changed files with 44 additions and 14 deletions

View File

@ -86,7 +86,7 @@ export default (app: Router) => {
code: 200, code: 200,
data: { data: {
username: authInfo.username, username: authInfo.username,
twoFactorActived: authInfo.twoFactorActived, twoFactorActivated: authInfo.twoFactorActivated,
}, },
}); });
} catch (e) { } catch (e) {
@ -182,4 +182,34 @@ export default (app: Router) => {
} }
}, },
); );
route.get(
'/user/notification',
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const authService = Container.get(AuthService);
const data = await authService.getNotificationMode();
res.send({ code: 200, data });
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
}
},
);
route.put(
'/user/notification',
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const authService = Container.get(AuthService);
const data = await authService.updateNotificationMode(req.body);
res.send({ code: 200, data });
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
}
},
);
}; };

View File

@ -47,7 +47,7 @@ export default class AuthService {
lastlogon, lastlogon,
lastip, lastip,
lastaddr, lastaddr,
twoFactorActived, twoFactorActivated,
} = content; } = content;
if ( if (
@ -72,7 +72,7 @@ export default class AuthService {
const { ip, address } = await getNetIp(req); const { ip, address } = await getNetIp(req);
if (username === cUsername && password === cPassword) { if (username === cUsername && password === cPassword) {
if (twoFactorActived && needTwoFactor) { if (twoFactorActivated && needTwoFactor) {
this.updateAuthInfo(content, { this.updateAuthInfo(content, {
isTwoFactorChecking: true, isTwoFactorChecking: true,
}); });
@ -83,7 +83,7 @@ export default class AuthService {
} }
const data = createRandomString(50, 100); const data = createRandomString(50, 100);
const expiration = twoFactorActived ? 30 : 3; const expiration = twoFactorActivated ? 30 : 3;
let token = jwt.sign({ data }, config.secret as any, { let token = jwt.sign({ data }, config.secret as any, {
expiresIn: 60 * 60 * 24 * expiration, expiresIn: 60 * 60 * 24 * expiration,
algorithm: 'HS384', algorithm: 'HS384',
@ -206,7 +206,7 @@ export default class AuthService {
secret: authInfo.twoFactorSecret, secret: authInfo.twoFactorSecret,
}); });
if (isValid) { if (isValid) {
this.updateAuthInfo(authInfo, { twoFactorActived: true }); this.updateAuthInfo(authInfo, { twoFactorActivated: true });
} }
return isValid; return isValid;
} }
@ -236,7 +236,7 @@ export default class AuthService {
public deactiveTwoFactor() { public deactiveTwoFactor() {
const authInfo = this.getAuthInfo(); const authInfo = this.getAuthInfo();
this.updateAuthInfo(authInfo, { this.updateAuthInfo(authInfo, {
twoFactorActived: false, twoFactorActivated: false,
twoFactorSecret: '', twoFactorSecret: '',
}); });
return true; return true;

View File

@ -494,7 +494,7 @@ main() {
;; ;;
resettfa) resettfa)
echo -e "## 开始执行... $begin_time\n" >>$log_path echo -e "## 开始执行... $begin_time\n" >>$log_path
auth_value=$(cat $file_auth_user | jq '.twoFactorActived =false' -c) auth_value=$(cat $file_auth_user | jq '.twoFactorActivated =false' -c)
echo -e "禁用两步验证成功 \n $auth_value" >>$log_path echo -e "禁用两步验证成功 \n $auth_value" >>$log_path
echo "$auth_value" >$file_auth_user echo "$auth_value" >$file_auth_user
;; ;;

View File

@ -9,7 +9,7 @@ const { Title, Link } = Typography;
const SecuritySettings = ({ user, userChange }: any) => { const SecuritySettings = ({ user, userChange }: any) => {
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [twoFactorActived, setTwoFactorActived] = useState<boolean>(); const [twoFactorActivated, setTwoFactorActivated] = useState<boolean>();
const [twoFactoring, setTwoFactoring] = useState(false); const [twoFactoring, setTwoFactoring] = useState(false);
const [twoFactorInfo, setTwoFactorInfo] = useState<any>(); const [twoFactorInfo, setTwoFactorInfo] = useState<any>();
const [code, setCode] = useState<string>(); const [code, setCode] = useState<string>();
@ -32,7 +32,7 @@ const SecuritySettings = ({ user, userChange }: any) => {
}; };
const activeOrDeactiveTwoFactor = () => { const activeOrDeactiveTwoFactor = () => {
if (twoFactorActived) { if (twoFactorActivated) {
deactiveTowFactor(); deactiveTowFactor();
} else { } else {
getTwoFactorInfo(); getTwoFactorInfo();
@ -45,7 +45,7 @@ const SecuritySettings = ({ user, userChange }: any) => {
.put(`${config.apiPrefix}user/two-factor/deactive`) .put(`${config.apiPrefix}user/two-factor/deactive`)
.then((data: any) => { .then((data: any) => {
if (data.data) { if (data.data) {
setTwoFactorActived(false); setTwoFactorActivated(false);
userChange(); userChange();
} }
}) })
@ -62,7 +62,7 @@ const SecuritySettings = ({ user, userChange }: any) => {
if (data.data) { if (data.data) {
message.success('激活成功'); message.success('激活成功');
setTwoFactoring(false); setTwoFactoring(false);
setTwoFactorActived(true); setTwoFactorActivated(true);
userChange(); userChange();
} else { } else {
message.success('验证失败'); message.success('验证失败');
@ -86,7 +86,7 @@ const SecuritySettings = ({ user, userChange }: any) => {
}; };
useEffect(() => { useEffect(() => {
setTwoFactorActived(user && user.twoFactorActived); setTwoFactorActivated(user && user.twoFactorActivated);
}, [user]); }, [user]);
return twoFactoring ? ( return twoFactoring ? (
@ -198,10 +198,10 @@ const SecuritySettings = ({ user, userChange }: any) => {
</div> </div>
<Button <Button
type="primary" type="primary"
danger={twoFactorActived} danger={twoFactorActivated}
onClick={activeOrDeactiveTwoFactor} onClick={activeOrDeactiveTwoFactor}
> >
{twoFactorActived ? '禁用' : '启用'} {twoFactorActivated ? '禁用' : '启用'}
</Button> </Button>
</> </>
); );