Created
July 21, 2015 13:29
-
-
Save AlexFrazer/6b5c36ecac3fd34d1ea8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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