API session: CoffeeScript → ES6.

This commit is contained in:
Julien Fontanet
2015-03-09 15:28:20 +01:00
parent 34e8f57f7d
commit e6154db6e5
2 changed files with 67 additions and 61 deletions

View File

@@ -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
View 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};