API session: CoffeeScript → ES6.
This commit is contained in:
@@ -1,61 +0,0 @@
|
||||
{$coroutine, $wait} = require '../fibers-utils'
|
||||
|
||||
#=====================================================================
|
||||
|
||||
# Signs a user in with its email/password.
|
||||
exports.signInWithPassword = $coroutine ({email, password}) ->
|
||||
@throw 'ALREADY_AUTHENTICATED' if @session.has 'user_id'
|
||||
|
||||
# Gets the user.
|
||||
user = $wait @users.first {email}
|
||||
|
||||
# Invalid credentials if the user does not exists or if the password
|
||||
# does not check.
|
||||
@throw 'INVALID_CREDENTIAL' unless user and $wait user.checkPassword password
|
||||
|
||||
# Stores the user identifier in the session.
|
||||
@session.set 'user_id', user.get 'id'
|
||||
|
||||
# Returns the user.
|
||||
return @getUserPublicProperties user
|
||||
exports.signInWithPassword.params = {
|
||||
email: { type: 'string' }
|
||||
password: { type: 'string' }
|
||||
}
|
||||
|
||||
# Signs a user in with a token.
|
||||
exports.signInWithToken = $coroutine ({token}) ->
|
||||
@throw 'ALREADY_AUTHENTICATED' if @session.has 'user_id'
|
||||
|
||||
# Gets the token.
|
||||
token = $wait @tokens.first token
|
||||
@throw 'INVALID_CREDENTIAL' unless token?
|
||||
|
||||
# Stores the user and the token identifiers in the session.
|
||||
user_id = token.get('user_id')
|
||||
@session.set 'token_id', token.get 'id'
|
||||
@session.set 'user_id', user_id
|
||||
|
||||
# Returns the user.
|
||||
user = $wait @users.first user_id
|
||||
return @getUserPublicProperties user
|
||||
exports.signInWithToken.params = {
|
||||
token: { type: 'string' }
|
||||
}
|
||||
|
||||
exports.signOut = $coroutine ->
|
||||
@session.unset 'token_id'
|
||||
@session.unset 'user_id'
|
||||
|
||||
return true
|
||||
|
||||
# Gets the the currently signed in user.
|
||||
exports.getUser = $coroutine ->
|
||||
id = @session.get 'user_id', null
|
||||
|
||||
# If the user is not signed in, returns null.
|
||||
return null unless id?
|
||||
|
||||
# Returns the user.
|
||||
user = $wait @users.first id
|
||||
return @getUserPublicProperties user
|
||||
67
src/api/session.js
Normal file
67
src/api/session.js
Normal file
@@ -0,0 +1,67 @@
|
||||
import {InvalidCredential, AlreadyAuthenticated} from '../api-errors';
|
||||
|
||||
import {$coroutine as coroutine, $wait as wait} from '../fibers-utils';
|
||||
|
||||
//====================================================================
|
||||
|
||||
let signInWithPassword = coroutine(function ({email, password}) {
|
||||
if (this.session.has('user_id')) {
|
||||
throw new AlreadyAuthenticated();
|
||||
}
|
||||
|
||||
let user = wait(this.users.first({email}));
|
||||
if (!user || wait(user.checkPassword(password))) {
|
||||
throw new InvalidCredential();
|
||||
}
|
||||
|
||||
this.session.set('user_id', user.get('id'));
|
||||
|
||||
// Returns the user.
|
||||
return this.getUserPublicProperties(user);
|
||||
});
|
||||
|
||||
signInWithPassword.params = {
|
||||
email: { type: 'string' },
|
||||
password: { type: 'string' },
|
||||
};
|
||||
|
||||
export {signInWithPassword};
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
let signInWithToken = coroutine(function ({token: tokenId}) {
|
||||
if (this.session.has('user_id')) {
|
||||
throw new AlreadyAuthenticated();
|
||||
}
|
||||
|
||||
let token = wait(this.tokens.first(tokenId));
|
||||
if (!token) {
|
||||
throw new InvalidCredential();
|
||||
}
|
||||
|
||||
let userId = token.get('user_id');
|
||||
this.session.set('user_id', userId);
|
||||
this.session.set('token_id', token.get('id'));
|
||||
|
||||
// Returns the user.
|
||||
return this.getUserPublicProperties(wait(this.users.first(userId)));
|
||||
});
|
||||
|
||||
signInWithToken.params = {
|
||||
token: { type: 'string' },
|
||||
};
|
||||
|
||||
export {signInWithToken};
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
let getUser = coroutine(function () {
|
||||
let userId = this.session.get('user_id');
|
||||
|
||||
return userId === undefined ?
|
||||
null :
|
||||
this.getUserPublicProperties(wait(this.users.first(userId)))
|
||||
;
|
||||
});
|
||||
|
||||
export {getUser};
|
||||
Reference in New Issue
Block a user