mirror of
https://github.com/r-ca/kidshift-be.git
synced 2024-10-25 16:14:02 +00:00
register修正
This commit is contained in:
parent
504ebeed08
commit
07efcde405
|
@ -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です" });
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue
Block a user