이메일 인증 기능을 완료하고 회원 가입 기능을 구현 하였다.
기능 자체를 구현하기 전에 비밀번호를 해싱을 먼저 구현했다.
// models/User 파일
import crypto from "crypto";
("use strict");
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define(
"User",
{
nickName: {
allowNull: false,
type: DataTypes.STRING(12),
unique: true
},
email: {
allowNull: true,
type: DataTypes.STRING,
unique: true
},
password: {
allowNull: true,
type: DataTypes.STRING
},
profileImage: {
allowNull: true,
type: DataTypes.STRING
},
provider: {
allowNull: true,
type: DataTypes.STRING
},
admin: {
allowNull: false,
type: DataTypes.BOOLEAN
}
},
{
hooks: {
beforeCreate: data => {
if (data.password.length) {
const shasum = crypto.createHash("sha1");
shasum.update(data.password);
data.password = shasum.digest("hex");
}
},
beforeFind: data => {
if (data.where.password.length) {
const shasum = crypto.createHash("sha1");
shasum.update(data.where.password);
data.where.password = shasum.digest("hex");
}
}
}
}
);
User.asshociate = function(models) {
// associations can be defined here
User.hasMany(models.Pet);
User.hasMany(models.Board);
User.hasMany(models.Comment);
User.hasMany(models.Like);
User.hasMany(models.Sos);
};
return User;
};
sequelize의 hooks를 이용해 crypto 모듈을 통해 password를 해싱하는 작업을 해주었다.
테이블에 데이터가 만들어질 때 비밀번호를 해싱하고 로그인 할 때 테이블을 찾을 때
다시 비밀번호를 해싱하여 일치하는 데이터를 찾는 작업을 해주었다.
비밀번호 해싱하는 기능을 구현할 때 사실 해싱 알고리즘에 대해서 잘 모르는 상태로 sha1
알고리즘이
눈에 띄어서 쓰게 되었는데 나중에 알고보니 보안에 취약하다고 알게 되었고
패스워드 저장을 위해 만들어진 bcrypt
알고리즘을 알게 되었다.
// 스키마
type Mutation {
localsignUp(
nickName: String!
email: String
password: String
profileImage: String
provider: String
admin: Boolean!
): signUpHandle!
}
type signUpHandle {
status: Boolean!
err: String
}
회원 가입을 위한 스키마 파일에 Mutation
을 작성해주었다.
//레졸버 파일
import { User } from "../../../../../models/index";
export default {
Mutation: {
localsignUp: (_, args) => {
const {
nickName,
email = "",
password = "",
profileImage = "https://d2x5ku95bkycr3.cloudfront.net/App_Themes/Common/images/profile/0_200.png",
provider = "local",
admin = false
} = args;
return User.create({
nickName,
email,
password,
profileImage,
provider,
admin
})
.then(data => {
if (data) {
return {
status: true,
err: null
};
}
return {
status: false,
err: "failed account create"
};
})
.catch(err => {
return {
status: false,
err: err.errors[0].message
};
});
}
}
};
회원 가입을 위한 레졸버 파일에 작성해주었고 데이터 베이스에 성공적으로 저장하게 되면
status를 true로 반환해주는 코드를 작성하였다.