diff --git a/controller/users/google.js b/controller/users/google.js new file mode 100644 index 0000000..3840773 --- /dev/null +++ b/controller/users/google.js @@ -0,0 +1,76 @@ +require('dotenv').config(); +const { user } = require('../../models'); +const { OAuth2Client } = require('google-auth-library'); +const client = new OAuth2Client(process.env.googleClientId); +const jwt = require('jsonwebtoken'); + +module.exports = { + post: (req, res) => { + const updateToken = payload => { + const { sub, name, email } = payload; + const token = jwt.sign({ + id: sub, + name, + email + }, 'testSecret'); + + user.update({ + token: token + }, { + where: { + googleId: sub + } + }).catch(err => console.log(err)); + + return token; + }; + + const insertUserIntoDB = payload => { + const { sub, name, email } = payload; + const token = jwt.sign({ + id: sub, + name, + email + }, 'testSecret'); + + user.create({ + googleId: sub, + email: email, + username: name, + token: token + }).catch(err => console.log(err)); + + return token; + }; + + async function verify() { + const ticket = await client.verifyIdToken({ + idToken: req.body.tokenId + }); + const payload = ticket.getPayload(); + const googleId = payload['sub']; + + user.findOne({ + attributes: ['token'] + }, { + where: { + googleId: googleId + } + }).then(result => { + let token = ''; + console.log(result); + if (result.length > 0) { + console.log('DB에 있는 유저'); + token = updateToken(payload); + } else { + console.log('DB에 없는 유저'); + token = insertUserIntoDB(payload); + } + res.send({ token }); + }).catch(err => console.log(err)); + }; + + verify().then(() => { }).catch(console.error); + } +}; + diff --git a/controller/users/index.js b/controller/users/index.js index 30fb200..8bab438 100644 --- a/controller/users/index.js +++ b/controller/users/index.js @@ -5,5 +5,6 @@ module.exports = { signin: require('./signin'), signout: require('./signout'), signup: require('./signup'), - remove: require('./remove') + remove: require('./remove'), + google: require('./google') }; \ No newline at end of file diff --git a/controller/users/signin.js b/controller/users/signin.js index 8df2c82..fcba2c1 100644 --- a/controller/users/signin.js +++ b/controller/users/signin.js @@ -1,8 +1,4 @@ -require('dotenv').config(); const { user } = require('../../models'); -const { OAuth2Client } = require('google-auth-library'); -const client = new OAuth2Client(process.env.googleClientId); -const jwt = require('jsonwebtoken'); module.exports = { post: (req, res) => { @@ -24,70 +20,5 @@ module.exports = { }).catch(err => res.status(500).send(err)); } - const updateToken = payload => { - const { sub, name, email } = payload; - const token = jwt.sign({ - id: sub, - name, - email - }, 'testSecret'); - - user.update({ - token: token - }, { - where: { - googleId: sub - } - }).catch(err => console.log(err)); - - return token; - }; - - const insertUserIntoDB = payload => { - const { sub, name, email } = payload; - const token = jwt.sign({ - id: sub, - name, - email - }, 'testSecret'); - - user.create({ - googleId: sub, - email: email, - username: name, - token: token - }).catch(err => console.log(err)); - - return token; - }; - - async function verify() { - const ticket = await client.verifyIdToken({ - idToken: req.body.it - }); - const payload = ticket.getPayload(); - const googleId = payload['sub']; - - user.findAll({ - attributes: ['token'] - }, { - where: { - googleId: googleId - } - }).then(result => { - let token = ''; - console.log(result); - if (result.length > 0) { - console.log('DB에 있는 유저'); - token = updateToken(payload); - } else { - console.log('DB에 없는 유저'); - token = insertUserIntoDB(payload); - } - res.send({ token }); - }).catch(err => console.log(err)); - }; - - verify().then(() => { }).catch(console.error); } }; \ No newline at end of file diff --git a/models/user.js b/models/user.js index 1733288..ceb4189 100644 --- a/models/user.js +++ b/models/user.js @@ -22,12 +22,14 @@ module.exports = (sequelize, DataTypes) => { }, { hooks: { beforeCreate: data => { - let shasum = crypto.createHmac('sha512', 'to-go-SecRet!$'); - shasum.update(data.password); - data.password = shasum.digest('hex'); + if (data.password) { + let shasum = crypto.createHmac('sha512', 'to-go-SecRet!$'); + shasum.update(data.password); + data.password = shasum.digest('hex'); + } }, beforeFind: data => { - if (data.where.password) { + if (Object.keys(data).indexOf('where') !== -1) { let shasum = crypto.createHmac('sha512', 'to-go-SecRet!$'); shasum.update(data.where.password); data.where.password = shasum.digest('hex'); diff --git a/routes/users.js b/routes/users.js index e3f18df..d102a14 100644 --- a/routes/users.js +++ b/routes/users.js @@ -10,5 +10,6 @@ router.get('/info', usersController.info.get); router.patch('/editinfo', usersController.editinfo.patch); router.post('/checkpwd', usersController.checkpwd.post); router.post('/remove', usersController.remove.post); +router.post('/google', usersController.google.post); module.exports = router; \ No newline at end of file