Permalink
Browse files

added login by username password

  • Loading branch information...
1 parent c39f877 commit 6522654cf3f70d5272f0a61d8ea4e7d87ee52ea0 @JustinBeckwith JustinBeckwith committed Feb 16, 2012
Showing with 120 additions and 21 deletions.
  1. +0 −5 routes/account.js
  2. +71 −10 server.js
  3. +12 −6 views/account/login.jade
  4. +33 −0 views/account/register.jade
  5. +4 −0 views/layout.jade
View
@@ -6,11 +6,6 @@ module.exports = function(app) {
res.render('account/account', { user: req.user });
});
- // login page
- app.get('/login', function(req, res) {
- res.render('account/login', { user: req.user });
- });
-
// logout
app.get('/logout', function(req, res) {
res.redirect('/');
View
@@ -12,33 +12,39 @@ var express = require('express')
, nconf = require('nconf');
-
-
+/**
+* CONFIGURATION
+* -------------------------------------------------------------------------------------------------
+* load configuration settings from settings.json. Contains keys for OAuth logins. See
+* settings.example.json.
+**/
+nconf.file({file: 'settings.json'});
/**
-* OAUTH FEDERATED IDENTITY
+* EVERYAUTH AUTHENTICATION
* -------------------------------------------------------------------------------------------------
* allows users to log in and register using OAuth
**/
-
-nconf.file({file: 'settings.json'});
-
everyauth.debug = true;
// Configure Facebook auth
var usersById = {},
nextUserId = 0,
usersByFacebookId = {},
- usersByTwitId = {};
+ usersByTwitId = {},
+ usersByLogin = {
+ 'justbe@microsoft.com': addUser({ email: 'justbe@microsoft.com', password: 'azure'})
+ };
everyauth.
everymodule.
findUserById(function (id, callback) {
callback(null, usersById[id]);
});
+// facebook authentication
everyauth.
facebook.
appId(nconf.get('facebook:applicationId')).
@@ -51,6 +57,7 @@ everyauth.
}).
redirectPath('/');
+// twitter authentication
everyauth
.twitter
.consumerKey(nconf.get('twitter:consumerKey'))
@@ -60,11 +67,65 @@ everyauth
})
.redirectPath('/');
+// username / password authentication
+everyauth
+ .password
+ .loginWith('email')
+ .getLoginPath('/login')
+ .postLoginPath('/login')
+ .loginView('account/login')
+ .loginLocals( function (req, res, done) {
+ setTimeout( function () {
+ done(null, {
+ title: 'login. '
+ });
+ }, 200);
+ })
+ .authenticate( function (login, password) {
+ var errors = [];
+ if (!login) errors.push('Missing login');
+ if (!password) errors.push('Missing password');
+ if (errors.length) return errors;
+ var user = usersByLogin[login];
+ if (!user) return ['Login failed'];
+ if (user.password !== password) return ['Login failed'];
+ return user;
+ })
+ .getRegisterPath('/register')
+ .postRegisterPath('/register')
+ .registerView('account/register')
+ .registerLocals( function (req, res, done) {
+ setTimeout( function () {
+ done(null, {
+ title: 'Register. '
+ });
+ }, 200);
+ })
+ .validateRegistration( function (newUserAttrs, errors) {
+ var login = newUserAttrs.login;
+ if (usersByLogin[login]) errors.push('Login already taken');
+ return errors;
+ })
+ .registerUser( function (newUserAttrs) {
+ var login = newUserAttrs[this.loginKey()];
+ return usersByLogin[login] = addUser(newUserAttrs);
+ })
+ .loginSuccessRedirect('/')
+ .registerSuccessRedirect('/');
+// add a user to the in memory store of users. If you were looking to use a persistent store, this
+// would be the place to start
function addUser (source, sourceUser) {
- var user = {id: ++nextUserId, source: sourceUser};
- usersById[nextUserId] = user;
- return user;
+ var user;
+ if (arguments.length === 1) {
+ user = sourceUser = source;
+ user.id = ++nextUserId;
+ return usersById[nextUserId] = user;
+ } else { // non-password-based
+ user = usersById[++nextUserId] = {id: nextUserId};
+ user[source] = sourceUser;
+ }
+ return user;
}
@@ -3,24 +3,29 @@ section(class="main-content")
h1 Log in.
h2 Enter your user name and password below.
+ - if ('undefined' !== typeof errors && errors.length)
+ ul#errors
+ - each error in errors
+ li.error= error
+
section(id="loginForm")
form(method="post", action="/login")
fieldset
legend Log in to Your Account
ol
li(class="username")
- label(for="username") Username:
- input(type="text", name="username")
+ label(for=everyauth.password.loginFormFieldName) Email:
+ input(type="text", name=everyauth.password.loginFormFieldName, value=email)
li(class="password")
- label(for="password") Password:
- input(type="password", name="password")
+ label(for=everyauth.password.passwordFormFieldName) Password:
+ input(type="password", name=everyauth.password.passwordFormFieldName)
li(class="remember-me")
input(type="checkbox", id="rememberMe", name="rememberMe", value="true")
label(class="checkbox", for="rememberMe") Remember me?
input(type="submit", value="Login")
- a(href="/account/register") Don't have an Account?
- a(href="/account/forgotPassword") Did you forget your password?
+ a(href="/register") Don't have an Account?
+ a(href="/forgotPassword") Did you forget your password?
section(class="social", id="socialLoginForm")
form(method="post")
@@ -30,6 +35,7 @@ section(class="main-content")
img(src=base + '/images/facebookLogin.png')
br
+ br
a(href='/auth/twitter', style='border: 0px')
img(style='border: 0px', src='https://si0.twimg.com/images/dev/buttons/sign-in-with-twitter-l.png')
@@ -0,0 +1,33 @@
+section(class="main-content")
+ hgroup(class="title")
+ h1= title
+ h2 Use the form(below to create a new account
+
+ - if ('undefined' !== typeof errors && errors.length)
+ ul#errors
+ - each error in errors
+ li.error= error
+
+ form(method="post")
+ fieldset
+ legend Registration Form
+ ol
+ li(class="email")
+ label(for=everyauth.password.loginFormFieldName) Email address
+ input(type="text", name=everyauth.password.loginFormFieldName, value=userParams[everyauth.password.loginFormFieldName])
+ li(class="password")
+ label(for="password") Password
+ input(type="password", name=everyauth.password.passwordFormFieldName)
+ li(class="confirm-password")
+ label(for="confirmPassword") Confirm password
+ input(type="password", id="confirmPassword", name="confirmPassword")
+ li(class="recaptcha")
+ div(class="message-info")
+ p To enable CAPTCHA verification...
+
+ input(type="submit", value="Register")
+ p
+ | Did you know you can use other services to log in to this site?
+ | Click
+ a(href="/login") here
+ | to log in using one of the supported services.
View
@@ -24,11 +24,15 @@ html
-if(!everyauth.loggedIn)
li
a(href="/login") Log in
+ li
+ a(href="/register") Register
-else
-if(everyauth.facebook)
li Welcome !{everyauth.facebook.user.first_name} !{everyauth.facebook.user.last_name}!
-else if(everyauth.twitter)
li Welcome !{everyauth.twitter.user.name}!
+ -else if(everyauth.password)
+ li Welcome !{everyauth.user.email}!
li
a(href="/logout") Logout
nav

0 comments on commit 6522654

Please sign in to comment.