-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from zoaby/feature-branch
backend authentication set-up
- Loading branch information
Showing
5 changed files
with
615 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// requiring modules for mongoose | ||
const mongoose = require('mongoose'); | ||
const db = mongoose.connection; | ||
mongoose.connect('mongodb://localhost/Mine'); | ||
|
||
db.on('error', console.error.bind(console, 'connection error:')); | ||
db.once('open', function() { | ||
console.log('connect'); | ||
}); | ||
|
||
const userSchema = mongoose.Schema({ | ||
userName: String, | ||
passWord: String | ||
}); | ||
const urlSchema = mongoose.Schema({ | ||
url: String, | ||
category: String, | ||
userName: String, | ||
likes: Number, | ||
comments: [String] | ||
}) | ||
const Url = mongoose.model('Url', urlSchema) | ||
const User = mongoose.model('User', userSchema); | ||
|
||
|
||
|
||
module.exports.User = User; | ||
module.exports.Url = Url; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
exports.createSession = (req, res, newUser) => { | ||
return req.session.regenerate(() => { | ||
req.session.user = newUser; | ||
res.redirect('/'); | ||
}); | ||
} | ||
exports.isLoggedIn = (req, res) => { | ||
return req.session ? !!req.session.user : false; | ||
}; | ||
exports.checkUser = (req, res, next) => { | ||
if (!exports.isLoggedIn(req)) { | ||
res.status(404) | ||
res.redirect('/') | ||
} | ||
else { | ||
next(); | ||
} | ||
}; |
Oops, something went wrong.