Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. const express = require('express')
  2. const app = express()
  3. const passport = require('passport');
  4. const Auth0Strategy = require('passport-auth0');
  5. const session = require('express-session');
  6. const secured = require('C:\\Users\\irina\\WebstormProjects\\webdb\\lib\\middleware\\secured.js');
  7. const port = 8080
  8. const bodyParser = require("body-parser");
  9.  
  10. app.use(bodyParser.json())
  11. app.use(bodyParser.urlencoded({
  12. extended: true
  13. }))
  14. const request = require('C:\\Users\\irina\\WebstormProjects\\webdb\\public\\request.js')
  15. const { Pool, Client } = require('pg')
  16.  
  17.  
  18. const pool = new Pool({
  19. user: 'db_admin',
  20. host: 'web-dbs-instances.civdu8egjyug.eu-west-2.rds.amazonaws.com',
  21. database: 'web_db',
  22. password: 'QAZplm123)(*',
  23. port: 5432,
  24. })
  25.  
  26. app.set("view engine",'ejs');
  27.  
  28.  
  29. pool.query('SELECT NOW()', (err, res) => {
  30. console.log(err, res)
  31. pool.end()
  32. })
  33.  
  34. const client = new Client({
  35. user: 'db_admin',
  36. host: 'web-dbs-instances.civdu8egjyug.eu-west-2.rds.amazonaws.com',
  37. database: 'web_db',
  38. password: 'QAZplm123)(*',
  39. port: 5432,
  40. })
  41. client.connect()
  42.  
  43. /*client.query('select * from events', (err, res) => {
  44. console.log(err, res)
  45. client.end()
  46. })*/
  47.  
  48.  
  49. // config express-session
  50. var sess = {
  51. secret: 'CHANGE THIS SECRET<--------',
  52. cookie: {},
  53. resave: false,
  54. saveUninitialized: true
  55. };
  56.  
  57. if (app.get('env') === 'production') {
  58. sess.cookie.secure = true; // serve secure cookies, requires https
  59. }
  60. //Внизу что-то интересное
  61. // Configure Passport to use Auth0
  62. const strategy = new Auth0Strategy(
  63. {
  64. domain: 'cehan.eu.auth0.com',
  65. clientID: 'RQmn4EorkSAFB1xXMD5rRheMDo1MARyE',
  66. clientSecret: 'KTIYR8OcY27pbd7LF-mgGjv8TEVeixpnvpGmdDHI_71D7eCrr7qalQ_TtTMyp64z',
  67. callbackURL:
  68. process.env.AUTH0_CALLBACK_URL || 'http://localhost:8080/callback'
  69. },
  70. function (accessToken, refreshToken, extraParams, profile, done) {
  71. console.log(profile)
  72. let userid = profile.id;
  73. client.query('select count(client_id) as count from clients where client_id=$1',[userid], (err,res) => {
  74. if (err) {
  75. console.log(err.stack)
  76. }
  77.  
  78. console.log(res.rows[0].count)
  79. if (res.rows[0].count == 0) {
  80. let lname = profile.name.familyName;
  81. let fname = profile.name.givenName;
  82. let ct = 1;
  83. client.query('insert into clients (client_id,last_name,first_name,city_id) values ($1,$2,$3,$4)', [userid, lname, fname, ct], (err, res) => {
  84. if (err) {
  85. console.log(err.stack)
  86. }
  87. })
  88. }
  89. })
  90. // accessToken is the token to call Auth0 API (not needed in the most cases)
  91. // extraParams.id_token has the JSON Web Token
  92. // profile has all the information from the user
  93. return done(null, profile)
  94. }
  95. )
  96. passport.use(strategy);
  97. //Внизу что-то интересное
  98.  
  99. app.use(session(sess));
  100. app.use(passport.initialize());
  101. app.use(passport.session());
  102. //Внизу что-то интересное
  103. passport.serializeUser(function (user, done) {
  104. done(null, user);
  105. });
  106.  
  107. passport.deserializeUser(function (user, done) {
  108. done(null, user);
  109. });
  110.  
  111. app.get('/login', passport.authenticate('auth0', {
  112. scope: 'openid email profile'
  113. }), function (req, res) {
  114. res.redirect('/');
  115. });
  116.  
  117. app.get('/callback', function (req, res, next) {
  118. passport.authenticate('auth0', function (err, user, info) {
  119. if (err) { return next(err); }
  120. if (!user) { return res.redirect('/login'); }
  121. req.logIn(user, function (err) {
  122. if (err) { return next(err); }
  123. const returnTo = req.session.returnTo;
  124. delete req.session.returnTo;
  125. res.redirect(returnTo || '/');
  126. });
  127. })(req, res, next);
  128. });
  129.  
  130. app.get('/logout',(req,res)=>{
  131. req.logout()
  132. res.redirect('/')
  133. });
  134.  
  135.  
  136. app.get('/',(req, res) =>{
  137. console.log(req.isAuthenticated())
  138. res.sendFile(__dirname + '/public/index.html')
  139. })
  140. app.get('/about.html',(req, res) =>{
  141. console.log(req.isAuthenticated())
  142. res.sendFile(__dirname + '/public/about.html')
  143. })
  144. app.get('/index.html',(req, res) =>{
  145. console.log(req.isAuthenticated())
  146. res.sendFile(__dirname + '/public/index.html')
  147. })
  148.  
  149. app.get('/events',request.funevents);
  150.  
  151. app.get('/profile',request.funprofile);
  152.  
  153. app.get('/check', (req,res) => {
  154. res.send(req.isAuthenticated())
  155. });
  156.  
  157. app.post('/confirm', (req, res) => {
  158. console.log(req.body);
  159. let isDone = true;
  160. let id = req.body.eventid;
  161. let clnt = req.user.user_id;
  162. client.query('select count(id) as count from events_clients where client_id=$1 and event_id=$2',[clnt,id], (err,res) => {
  163. if (err) {
  164. console.log(err.stack)
  165. }
  166.  
  167. if (res.rows[0].count == 0) {
  168. client.query('insert into events_clients (event_id, client_id) values ($1,$2 )',[id,clnt], (err) => {
  169. if (err) {
  170. console.log(err.stack);
  171. }
  172. })
  173. }
  174. })
  175.  
  176.  
  177.  
  178. });
  179.  
  180. app.post('/reject', (req, res) => {
  181. let id = req.body.eventid;
  182. let clnt = req.user.user_id;
  183. client.query('delete from events_clients where event_id=$1 and client_id = $2',[id,clnt], (err) => {
  184. if (err) {
  185. console.log(err.stack)
  186. }
  187. });
  188. });
  189.  
  190. app.listen(port, () => console.log(`Server listening on port ${port}!`))
  191.  
  192. var Pusher = require('pusher');
  193.  
  194. var pusher = new Pusher({
  195. appId: '668435',
  196. key: '088e88ca8394e8035572',
  197. secret: 'dea11e8396f79a519e47',
  198. cluster: 'eu',
  199. encrypted: true
  200. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement