import { Request , Response } from 'express' ; import * as UserService from './service' ; export const checkId = async ( req , res ) => { try { const id = req . query . id as string ; const result = await UserService . idCheck ( id ) ; if ( result ) throw Error ( '이미 존재하는 아이디입니다.' ) ; res . json ( { result , message : '사용 가능한 아이디입니다.' } ) ; } catch ( err ) { res . json ( { result : false , message : err . message } ) ; } } ; export const checkLogin = async ( req , res ) => { try { const token = req . cookies . token ; if ( ! token ) { res . json ( { decoded : null } ) ; return ; } const decoded = UserService . verifyToken ( token ) as UserService . UserToken ; if ( decoded . id ) { const userInfo = await UserService . getUserInfo ( decoded . id ) ; res . json ( userInfo [ 0 ] ) ; return ; } res . json ( decoded ) ; } catch ( err ) { res . json ( { message : err . message } ) ; } } ; export const resetPwd = async ( req , res ) => { try { const { id , nickname , password } = req . body ; const result = await UserService . checkChangePasswordAvailable ( id , nickname , password ) ; res . json ( result ) ; } catch ( err ) { res . json ( { isChange : false , message : err . message } ) ; } } ; export const signIn = async ( req , res ) => { try { const { id , password } = req . body ; const result = await UserService . login ( { id , password } ) ; res . cookie ( 'token' , UserService . createUserToken ( id ) , { httpOnly : true , maxAge : 24 * 60 * 60 * 1000 } ) ; res . json ( result ) ; } catch ( err ) { res . json ( { isLogin : false , message : err . message } ) ; } } ; export const signUp = async ( req , res ) => { try { const { id , password , nickname , color } = req . body ; const result = await UserService . join ( { id , password , nickname , color } ) ; res . cookie ( 'token' , UserService . createUserToken ( id ) , { httpOnly : true , maxAge : 24 * 60 * 60 * 1000 } ) ; res . json ( result ) ; } catch ( err ) { res . json ( { isLogin : false , message : err . message } ) ; } } ; export const guestSignIn = ( req , res ) => { try { const { nickname , color } = req . body ; const result = UserService . enter ( ) ; res . cookie ( 'token' , UserService . createNonUserToken ( nickname , color ) , { httpOnly : true , maxAge : 24 * 60 * 60 * 1000 , } ) ; res . json ( result ) ; } catch ( err ) { res . json ( { isLogin : false , message : err . message } ) ; } } ; export const logOut = ( req , res ) => { res . clearCookie ( 'token' ) ; res . sendStatus ( 200 ) ; } ;