Skip to content
This repository has been archived by the owner on Sep 8, 2021. It is now read-only.

[kunlee, hyeojung, nahkim] - 고생하셨습니다 #3

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions back/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
33 changes: 33 additions & 0 deletions back/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const express = require('express');
const { sequelize } = require('./models/index');
require('dotenv').config();
const router = express.Router();
const authService = require("./auth.js");

const app = express();
app.use(express.json());

router.post(process.env.ENDPOINT_AUTH, (req, res) => {
authService.GoogleLogin(req, res);
})

sequelize.authenticate()
.then((res) => {
console.log("connected");
})
.catch((err) => {
console.error(err);
})

const driver = () => {
sequelize.sync().then(() => {
console.log('초기화 완료.');
}).catch((err) => {
console.error('초기화 실패!');
console.error(err);
});
};

app.listen(4242, () => {
driver();
});
38 changes: 38 additions & 0 deletions back/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { OAuth2Client } = require('google-auth-library');
const db = require('./models');
require('dotenv').config();
const client = new OAuth2Client(process.env.CLIENT_ID);
const models = require('./models');

const GoogleLogin = async (req, res) => {
const { token } = req.body;
const ticket = await client.verifyIdToken({
idToken: token,
audience: process.env.CLIENT_ID
});
const { name, email, picture } = ticket.getPayload();
const user = await db.user.upsert({
where: { email: email },
update: { name, picture },
create: { name, email, picture }
});
req.session.userId = user.id;
req.status(201);
res.json(user);
}

// app.use(async (req, res, next) => {
// const user = await db.user.findFirst({where: { id : req.session.userId }})
// req.user = user;
// next()
// })

// app.delete("", async (req, res) => {
// await req.session.destory()
// res.status(200)
// res.json({
// message: "Logged out successfully"
// })
// })

module.exports = { GoogleLogin };
10 changes: 10 additions & 0 deletions back/config/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
MYSQL_USER=root
MYSQL_PASS=
MYSQL_HOST=127.0.0.1
MYSQL_NAME=js-piscine-rush01
MYSQL_DIALET=mysql

ENDPOINT_AUTH=/auth

CLIENT_ID=971281759915-2ijhbm1bu64g7dukp0t7n406cjpcgnkd.apps.googleusercontent.com
CLIENT_PW=yo0yznSpyeZu4OSmk1WGa62Y
11 changes: 11 additions & 0 deletions back/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require('dotenv').config({path:'./config/.env'});

const development = {
"username": process.env.MYSQL_USER,
"password": process.env.MYSQL_PASS,
"database": process.env.MYSQL_NAME,
"host": process.env.MYSQL_HOST,
"dialect": process.env.MYSQL_DIALET
}

module.exports = {development};
11 changes: 11 additions & 0 deletions back/config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require('dotenv').config({path:'./config/.env'});

const development = {
"development": {
"username": process.env.MYSQL_USER,
"password": null,
"database": "js-piscine-rush01",
"host": "127.0.0.1",
"dialect": "mysql"
};
}
36 changes: 36 additions & 0 deletions back/models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const config = require(__dirname + '/../config/config.js')['development'];
const db = {};

let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
db[model.name] = model;
});

Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;
24 changes: 24 additions & 0 deletions back/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = (sequelize, DataTypes) => {
return sequelize.define('Users', {
nick: {
type: DataTypes.STRING(20),
allowNULL: false,
unique: true
},
email: {
type: DataTypes.STRING(100),
allowNULL: false
},
idToken: {
type: DataTypes.STRING(300),
allowNULL: true
},
token: {
type: DataTypes.STRING(300),
allowNULL: true
}
},
{
freezeTableName: true
})
};
Loading