-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
178 lines (161 loc) · 5.24 KB
/
Copy pathapp.js
File metadata and controls
178 lines (161 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
'use strict';
import express from 'express';
import session from 'express-session';
import stream from 'stream';
import fs from 'fs';
import jwt from 'jsonwebtoken';
import passport from 'passport';
import LocalStrategy from 'passport-local';
import FacebookStrategy from 'passport-facebook';
//import TwitterStrategy from 'passport-twitter';
//import GoogleStrategy from 'passport-google-oauth';
import Sequelize from 'sequelize';
import seqConfig from './database/sequelize/config';
import seqUser from './database/sequelize/models/user';
import seqProduct from './database/sequelize/models/product';
import seqProductOptions from './database/sequelize/models/productoptions';
import parsedCookies from './middlewares/parsedCookies';
import parsedQuery from './middlewares/parsedQuery';
import checkToken from './middlewares/checkToken';
import products from './models/products';
import users from './models/users';
import reviews from './models/reviews';
import creds from './config/creds';
const app = express();
const router = express.Router();
const env = 'development';
const sequelize = new Sequelize(seqConfig[env].database, seqConfig[env].username, seqConfig[env].password, {
host: seqConfig[env].host,
dialect: seqConfig[env].dialect,
logging: false
});
app.use(express.json());
app.use(parsedCookies);
app.use(parsedQuery);
// app.use(session({
// secret: 'secret',
// resave: false,
// saveUninitialized: true,
// cookie: {
// secure: true
// }
// }));
// app.use(passport.initialize());
// app.use(passport.session());
// passport.serializeUser(function(user, cb) {
// cb(null, user);
// });
// passport.deserializeUser(function(obj, cb) {
// cb(null, obj);
// });
let productId = products[products.length - 1].id,
path = './models/products.json';
passport.use(new LocalStrategy({
usernameField: 'login',
passwordField: 'password',
session: false
}, (username, password, done) => {
if (username == creds.login && password == creds.password) {
done(null, creds);
} else {
done(null, false, 'Wrong');
}
}));
passport.use(new FacebookStrategy({
clientID: "252598088602754",
clientSecret: '803d4d5f32e3a7368e0966b647aabae2',
callbackURL: "http://localhost:8000/auth/facebook/callback"
}, (accessToken, refreshToken, profile, cb) => {
return cb(null, profile);
}));
// passport.use(new TwitterStrategy({
// consumerKey: 'kNU0RQGZ6nU5cwFKppqizXVqU',
// consumerSecret: 'Bu1qK4kQXOsR7NkWhJuZppKjoekCx42Wh1hfNQAVDWaVoFoTS2',
// callbackURL: "http://localhost/auth/twitter/callback"
// }, (token, tokenSecret, profile, done) => {
// console.dir(profile)
// return done(null, profile);
// }));
app.get('/cookie', (request, response) => {
response.json(response.parsedCookies);
})
app.get('/query', (request, response) => {
response.json(response.parsedQuery)
});
app.post('/auth', passport.authenticate('local', { session: false }), (req, res) => {
let { password, login } = req.user;
if (password === creds.password && login === creds.login ) {
let token = jwt.sign(creds, "secret", { expiresIn: 100 });
res.setHeader("x-access-token", token);
res.json({
code: 200,
message: "OK",
data: {
user: {
email: "mail@yandex.ru",
username: "Andrew"
}
},
token: token
});
} else {
res.json({
code: 404,
message: "Not Found",
data: {}
})
}
});
app.get('/auth/facebook', passport.authenticate('facebook'));
app.get('/auth/facebook/callback', passport.authenticate('facebook', {
successRedirect: '/',
failureRedirecrt: '/auth'
}));
app.get('/auth/twitter', passport.authenticate('twitter'));
app.get('/auth/twitter/callback', passport.authenticate('twitter', {
successRedirect: '/',
failureRedirecrt: '/auth'
}));
const User = seqUser(sequelize, Sequelize),
Product = seqProduct(sequelize, Sequelize),
ProductOptions = seqProductOptions(sequelize, Sequelize);
Product.hasOne(ProductOptions, { foreignKey: 'productId' });
router
.get('/products', (req, res) => {
Product.findAll({
include: ProductOptions
}).then((data) => {
res.json(data)
});
})
.get('/products/:id', (req, res) => {
Product.findById(req.params.id, {
include: ProductOptions
}).then((data) => {
res.json(data)
});
})
.post('/products', (req, res) => {
let data = req.body;
Product.create(data, {
attributes: ['name', 'brand', 'price']
}).then(product => {
ProductOptions.create(Object.assign({productId: product.id}, data.options),
{
attributes: ['size', 'color', 'productId']
}).then(() => {
Product.findById(product.id, {
include: ProductOptions
}).then((data) => {
res.json(data)
});
});
});
})
.get('/users', (req, res) => {
Product.findAll().then((data) => {
res.json(data)
});
})
app.use('/api', checkToken, router);
export default app;