eCommAdmin is a simple Dockerize monolithic application, handling authentication via Google passportjs with Node.js.
OK, let's buy product in my store simulation here.

| Gallery page | Order page | Store Simulation |
|---|---|---|
![]() | ![]() | ![]() |
expressbody-parserexpress-sessioncookie-parserserve-staticconnect-ensure-loginpassport, passport-google-oauth2next, react, reduxsequelizeproject
.
├── components
│ └── [ANY STATELESS COMPONENTS]
├── helper
│ ├── reducer
│ │ └── [ANY REDUCERS OF EACH PAGE IN "*.js" FILE FORMAT]
│ ├── redux-store.js
│ └── RESTFul.js
├── pages
│ └── [ANY STATEFUL PAGE COMPONENTS]
├── public
│ └── images
│ └── [ANY IMAGES IN THE APPLICATION]
├── sequelize
│ ├── apis
│ │ └── database.js
│ ├── models
│ │ └── [ANY DATABASE SCHEMAS]
│ └── db-handler.js
├── src
│ └── theme.js
├── .babelrc
├── .dockerignore
├── .gitignore
├── Dockerfile
├── package.json
├── routes.js
└── server.js
const GoogleStrategy = require( 'passport-google-oauth2' ).Strategy;
//...
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_APP_ID,
clientSecret: process.env.GOOGLE_APP_SECRET,
callbackURL: `${process.env.HOST}/auth/google/callback`,
enableProof: true
},
function(accessToken, refreshToken, profile, cb) {
console.log('Access token : ', accessToken);
console.log('Refresh token : ', refreshToken);
console.log('Profile : ', profile);
cb(null, profile)
}
));
To get GOOGLE_APP_ID and GOOGLE_APP_SECRET, you must login to Google Cloud Platform to create your project first and then the credential. Please refer to this link https://console.developers.google.com/apis/credentials
passport.serializeUser(function(user, cb) {
cb(null, user);
});
passport.deserializeUser(function(obj, cb) {
cb(null, obj);
});
app.use(passport.initialize());
app.use(passport.session());
Create simple auth endpoint
// Calling API without auth via POST method
server.post('/api/v1/:name', (req, res) => {
// Handle APIs here
});
// ----- Set auth with Passport.js by using GoogleStrategy -----
// Scope :
// - https://developers.google.com/identity/protocols/oauth2/scopes
// - https://github.com/mstade/passport-google-oauth2#what-you-will-get-in-profile-response-
server.get('/login/google',
passport.authenticate('google', {
scope: [
'https://www.googleapis.com/auth/plus.login'
, 'https://www.googleapis.com/auth/plus.profile.emails.read'
, 'https://www.googleapis.com/auth/userinfo.email'
]
})
);
server.get('/auth/google/callback',
passport.authenticate('google', {
failureRedirect: '/login/google',
successRedirect: '/dashboard'
})
);
// ----- Access admin page with authentication -----
// Calling API with auth
server.post('/api/v1/:name', cslg.ensureLoggedIn('/login/google'), (req, res) => {
// Handle APIs here
});
// Page access
server.get('/dashboard', cslg.ensureLoggedIn('/login/google'), (req, res) => {
console.log('in get - user:', req.user);
return app.render(req, res, '/dashboard', { user:req.user })
})
Simple usage or run on production, I recommended you to use Dockerize on Heroku. Please see instruction below:
apk on node:12.16.1-alpine3.9--no-cacheopenssl, tzdataAsia/Bangkoknpm$ docker build -t patharanor/api-ecomm-gateway:SPECIAL_TAG .
GOOGLE_APP_ID - client ID of your Google AppGOOGLE_APP_SECRET - client secret of your Google AppHOST - host/instance that provides the servicePORT - port number of the serviceSESSION_SECRET_KEY - any wordBind port to outsite(left) via port number 3000.
$ docker run \
-p 3000:3000 \
-e APP_VERSION=0.4.0 \
-e GOOGLE_APP_ID=YOUR_GOOGLE_APP_ID \
-e GOOGLE_APP_SECRET=YOUR_GOOGLE_APP_SECRET \
-e HOST=YOUR_HOST \
-e PORT=3000 \
-e SESSION_SECRET_KEY=YOUR_SESSION_SECRET_KEY \
-e DB_USER=ekx...cc \
-e DB_PASS=90078c...........................fa87e226cd \
-e DB_HOST=ec2...................aws.com \
-e DB_PORT=5432 \
-e DB_NAME=da..........of2 \
patharanor/api-ecomm-gateway:SPECIAL_TAG
Login to DockerHub
$ docker login
Push it
$ docker push patharanor/api-ecomm-gateway:SPECIAL_TAG
Heroku
$ heroku login
$ heroku container:push web -a api-ecomm-gateway
$ heroku container:release web -a api-ecomm-gateway
Checking via Heroku logs:
$ heroku logs -a api-ecomm-gateway
Heroku
I recommended Postgres on Heroku, see more detail here
MIT
If this project help you reduce time to develop, you can give me a cup of coffee :)
Content type
Image
Digest
Size
82.7 MB
Last updated
almost 6 years ago
docker pull patharanor/api-ecomm-gateway