Skip to content

Instantly share code, notes, and snippets.

@AlexFrazer
Created July 21, 2015 13:29
Show Gist options
  • Save AlexFrazer/6b5c36ecac3fd34d1ea8 to your computer and use it in GitHub Desktop.
Save AlexFrazer/6b5c36ecac3fd34d1ea8 to your computer and use it in GitHub Desktop.
// client
// ApiV1.login returns a userId and token given the right email and password
Meteor.loginWithApi = function (email, password, callback) {
ApiV1.login(email, password)
.then(function (response) {
Accounts.callLoginMethod({
api: true,
token: response.data.token,
userId: response.data.userId
});
})
.catch(function (error) {
// to do, handle response error
});
}
// server
Accounts.registerLoginHandler(function (loginRequest) {
if (!loginRequest.api) return undefined;
if (!loginRequest.token || !loginRequest.userId) return null;
let userId = null;
let user = Users.findOne(loginRequest.userId);
if (!user) {
Users.insert({
_id: loginRequest.userId,
token: loginRequest.token
});
} else {
userId = user._id;
}
let stampedToken = Accounts._generateStampedLoginToken();
let hashedToken = Accounts._hashStampedToken(stampedToken);
Users.update(userId, {
$push: { "services.resume.loginTokens": stampedToken }
});
return {
userId: userId,
token: hashedToken
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment