Last active
December 20, 2018 07:38
-
-
Save rounakdatta/3ba853c6dfc4e30b896b79e64a45ebf1 to your computer and use it in GitHub Desktop.
Node.js app with Firebase-backed email-password authentication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const http = require('http') | |
const fs = require('fs'); | |
const express = require('express'); | |
const path = require('path'); | |
const bodyParser = require('body-parser'); | |
const request = require('request'); | |
const cookieParser = require('cookie-parser'); | |
const session = require('express-session'); | |
// firebase config | |
const firebase = require('firebase'); | |
var config = { | |
apiKey: "xxxx", | |
authDomain: "xxxx", | |
databaseURL: "xxxx", | |
projectId: "xxxx", | |
storageBucket: "xxxx", | |
messagingSenderId: "xxxx" | |
}; | |
var fbapp = firebase.initializeApp(config); | |
var db = fbapp.database(); | |
var auth = fbapp.auth(); | |
// app body-parser config | |
const app = express() | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(bodyParser.json()) | |
app.use(express.static(path.resolve(`${__dirname}/web/public`))); | |
console.log(`${__dirname}/web`); | |
app.use('*', (req, res, next) => { | |
console.log(`URL: ${req.baseUrl}`); | |
next(); | |
}); | |
app.use((req, res, next) => { | |
res.header('Access-Control-Allow-Origin', '*'); | |
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS'); | |
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept,X-access-token'); | |
next(); | |
}); | |
app.use((err, req, res, next) => { | |
if (err) { | |
res.send(err); | |
} | |
}); | |
app.set('views', __dirname + '/views'); | |
app.engine('html', require('ejs').renderFile); | |
app.use(express.static(__dirname + '/views/web/public')); | |
// app cookie-parser config | |
app.use(cookieParser()); | |
app.use(session({secret: 'xxxx'})); | |
// APIs start here | |
// home page | |
app.get('/', (req, res) => { | |
res.render('web/public/index.html'); | |
}); | |
// logout API | |
app.get('/logout', function(req, res) { | |
auth.signOut(); | |
res.clearCookie('currentUser'); | |
return res.redirect('/'); | |
}); | |
// register API | |
app.get('/register', function(req, res) { | |
if (req.cookies.currentUser) { | |
res.render('web/public/dashboard.html'); | |
} else { | |
res.render('web/public/register.html'); | |
} | |
}); | |
app.post('/register', function(req, res) { | |
var email = req.body.email; | |
var pwd = req.body.pwd; | |
auth.createUserWithEmailAndPassword(email, pwd) | |
.then(function(userData) { | |
console.log('registering and logging in'); | |
res.cookie('currentUser', auth.currentUser); | |
return res.redirect('/userdashboard'); | |
}) | |
.catch(function(error) { | |
if (error) { | |
console.log(error.message); | |
console.log(error); | |
return res.redirect('/'); | |
} | |
}); | |
}); | |
// login API | |
app.get('/login', function(req, res) { | |
if (req.cookies.currentUser) { | |
res.render('web/public/dashboard.html'); | |
} else { | |
res.render('web/public/login.html'); | |
} | |
}); | |
app.post('/login', function(req, res) { | |
var email = req.body.email; | |
var pwd = req.body.pwd; | |
auth.signInWithEmailAndPassword(email, pwd) | |
.then(function(userData) { | |
console.log('logging in'); | |
res.cookie('currentUser', auth.currentUser); | |
return res.redirect('/userdashboard'); | |
}) | |
.catch(function(error) { | |
if (error) { | |
console.log(error.message); | |
} | |
}); | |
}); | |
// user dashboard | |
app.get('/userdashboard', function(req, res) { | |
if (req.cookies.currentUser) { | |
res.render('web/public/dashboard.html'); | |
} else { | |
res.send('Unauthorized!') | |
} | |
}); | |
// server settings | |
var server = http.createServer(app); | |
server.listen(4000, function () { | |
console.log('Port 4000 - My superb Node.js App') | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>User Dashboard</title> | |
</head> | |
<body> | |
<h1>This is going to be a superb dashboard!</h1> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>My App</title> | |
</head> | |
<body> | |
<h1>My App</h1> | |
<a href="/register">Register</a><br> | |
<a href="/login">Login</a><br> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Login</title> | |
</head> | |
<body> | |
<h1>Login page</h1> | |
<form action="/login" method="POST"> | |
<label for="email">Email</label> | |
<input type="text" id="email" name="email"><br> | |
<label for="pwd">Password</label> | |
<input type="password" id="pwd" name="pwd"><br> | |
<input type="submit" id="submitButton" name="submitButton" value="Register"> | |
</form> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Register</title> | |
</head> | |
<body> | |
<h1>Register page</h1> | |
<form action="/register" method="POST"> | |
<label for="email">Email</label> | |
<input type="text" id="email" name="email"><br> | |
<label for="pwd">Password</label> | |
<input type="password" id="pwd" name="pwd"><br> | |
<input type="submit" id="submitButton" name="submitButton" value="Register"> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment