register修正

This commit is contained in:
ろむねこ 2024-06-25 15:46:50 +09:00
parent 504ebeed08
commit 07efcde405
Signed by: Fujimatsu
GPG Key ID: FA1F39A1BA37D168
2 changed files with 12 additions and 9 deletions

View File

@ -1,6 +1,7 @@
import { Router, Request, Response } from 'express';
import { registUser, loginUser } from '@src/services/parent/authService';
import Logger from '@src/logger';
import { TokenResponse } from '@src/models/Token';
const router = Router();
const logger = new Logger();
@ -9,10 +10,8 @@ logger.setTag('authRouter');
router.post('/register', (req: Request, res: Response) => {
const { email, password } = req.body;
registUser(email, password)
.then((token) => {
res.json({
"accessToken": token
});
.then((tokenResponse: TokenResponse) => {
res.status(201).json(tokenResponse);
})
.catch((err) => {
res.status(401).json({ message: "ユーザー登録失敗: すでに登録されているemailです" });

View File

@ -3,12 +3,13 @@ import bcrypt from "bcryptjs";
import { issueTokenByUserId } from "@src/utils/tokenUtils";
import { createHomeGroup } from "@src/services/homeGroupService";
import Logger from "@src/logger";
import { TokenResponse } from "@src/models/Token";
const logger = new Logger();
logger.setTag('authService');
async function registUser(email: string, password: string, homeGroupId?: string): Promise<String> {
async function registUser(email: string, password: string, homeGroupId?: string): Promise<TokenResponse> {
const hashedPassword = bcrypt.hashSync(password, 10);
if (!homeGroupId) { // TODO: 作成失敗したときにHomeGroupだけ残るのを防ぐ
@ -40,7 +41,10 @@ async function registUser(email: string, password: string, homeGroupId?: string)
});
const user = await registUser;
return issueTokenByUserId(user.id);
const response: TokenResponse = {
accessToken: await issueTokenByUserId(user.id)
};
return Promise.resolve(response)
}
async function loginUser(email: string, password: string): Promise<String | null> {
@ -49,10 +53,10 @@ async function loginUser(email: string, password: string): Promise<String | null
email: email
}
});
if(!user) {
if (!user) {
return null;
}
if(bcrypt.compareSync(password, user.password)) {
if (bcrypt.compareSync(password, user.password)) {
return issueTokenByUserId(user.id);
} else {
return null;