-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
71 lines (57 loc) · 2.43 KB
/
Copy pathauth.js
File metadata and controls
71 lines (57 loc) · 2.43 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
'use strict';
/**
*
* **Server Entrypoiny**
*
* Authentication Service will Handle all of the Authentication Queries for the Client in
* the Bliss app. It includes Generating Transient JWT Token, which are only used
* for Completing the Registration and Creating the Profile of Clients, and the Generation
* of Permanent JWT Tokens, which Authorizes Clients Throughout the Bliss App.
*
* Both the types of Token can be generated using Basic Auth, Google OAuth, and Facebook
* OAuth strategies.
*
*/
require('dotenv').config();
const AWS = require('aws-sdk');
const express = require('express');
const bodyParser = require('body-parser');
const signUpRoutes = require('./routes/routes');
const passport = require('passport');
const morgan = require('morgan');
const postgresConnection = require('./connections/PostgresConnection');
const strategiesConstructor = require('./strategies/Strategies');
const app = express();
const PORT = process.env.PORT || 5000;
const ENV = process.env.NODE_ENV;
if(ENV === 'development') console.log('##### SERVER IS RUNNING IN DEVELOPMENT MODE ######');
else if(ENV === 'production') console.log('##### SERVER IS RUNNING IN PRODUCTION MODE ######');
else {
console.error('No NODE_ENV is provided');
process.exit(1);
}
const postgresClient = postgresConnection(ENV);
AWS.config.getCredentials((err) => {
if(err) {
console.error(`CREDENTIALS NOT LOADED`);
process.exit(1);
}
else console.log(`##### AWS ACCESS KEY IS VALID #####`);
});
AWS.config.update({region: 'us-east-2'});
const strategies = strategiesConstructor();
const S3Client = new AWS.S3({apiVersion: '2006-03-01'});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(morgan('dev'));
app.use(passport.initialize());
app.use(passport.session());
passport.use('google-signup', strategies.facebookSignupStrategy(postgresClient, S3Client));
passport.use('facebook-signup', strategies.facebookSignupStrategy(postgresClient, S3Client));
postgresClient.authenticate()
.then(() => console.log(`Postgres Database Connected Successfully`))
.then(() => app.use('/client', signUpRoutes(postgresClient, S3Client)))
.then(() => app.get('/ping', (req, res) => res.send('OK')))
.then(() => console.log('Routes Established Successfully'))
.catch((err) => console.error(`Postgres Database Connection Failed! ${err}`));
app.listen(PORT, () => console.log(`Server Listening on Port ${PORT}`));