Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var uuid = require('node-uuid');
- var pb = require('../lib/pb');
- var PBException = require('../lib/pb-helpers').PBException;
- var utils = require('../lib/utils');
- module.exports.signup = function *(session, userID, envelope) {
- var command = envelope.userSignupRequest;
- var password = command.password;
- if (!password || password.length < 6) {
- throw new PBException(pb.PBError.ErrorType.INVALID_PASSWORD);
- }
- var email = command.email;
- if (!email) {
- throw new PBException(pb.PBError.ErrorType.INVALID_EMAIL);
- }
- var user = yield session.db.users.findOne({ email: email });
- if (user) {
- throw new PBException(pb.PBError.ErrorType.UNAVAILABLE_EMAIL);
- }
- user = yield session.db.users.insert({
- email: email,
- fname: command.fname,
- lname: command.lname,
- password: utils.sha256(password),
- type: pb.PBUser.UserType.BIDDER
- });
- var userSession = yield session.db.sessions.insert({
- userID: user._id,
- authToken: uuid.v4()
- });
- return new pb.PBCommandEnvelope({
- type: pb.PBCommandEnvelope.CommandType.USER_SIGNUP_RESPONSE,
- userSignupResponse: {
- user: { userID: user._id.toString() },
- session: {
- sessionID: userSession._id.toString(),
- authToken: userSession.authToken
- },
- email: email
- }
- });
- };
- module.exports.login = function *(session, userID, envelope) {
- var command = envelope.userLoginRequest;
- var email = command.email;
- if (!email) {
- throw new PBException(pb.PBError.ErrorType.INVALID_EMAIL);
- }
- var user = yield session.db.users.findOne({ email: email });
- if (!user) {
- throw new PBException(pb.PBError.ErrorType.NOT_FOUND);
- }
- if (utils.sha256(command.password || '') !== user.password) {
- throw new PBException(pb.PBError.ErrorType.UNAUTHORIZED);
- }
- var userSession = yield session.db.sessions.insert({
- userID: user._id,
- authToken: uuid.v4()
- });
- return new pb.PBCommandEnvelope({
- type: pb.PBCommandEnvelope.CommandType.USER_LOGIN_RESPONSE,
- userLoginResponse: {
- user: {
- email: user.email,
- userID: user._id.toString(),
- fname: user.fname || null,
- lname: user.lname || null
- },
- session: {
- sessionID: userSession._id.toString(),
- authToken: userSession.authToken
- },
- email: user.email
- }
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement