Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function login(username, password, callback) {
- var request = require('request');
- // DEBUG ONLY
- console.log('@@@ login - start @@@');
- console.log('username: ' + username);
- console.log('password: ' + password);
- username = username || '';
- console.log('!!!@@@@@@@@');
- console.log(configuration.ENDPOINT_LOCAL);
- console.log('!!!@@@@@@@@');
- var body, IDP_ENDPOINT;
- // var emailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
- // if (username.match(emailformat)) {
- // if (username.indexOf('@') >= 0) {
- if (/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/.test(username)) {
- IDP_ENDPOINT = configuration.ENDPOINT_LOCAL + "/api/v1/loginByEmail";
- console.log('using email');
- body = {
- email: username,
- password: password
- };
- } else {
- // eg. {"account_number":"1234"}
- IDP_ENDPOINT = configuration.ENDPOINT_LOCAL + "/api/v1/loginByAccountNumber";
- var params, account_number;
- try {
- params = JSON.parse(username);
- account_number = params.account_number;
- } catch (e) {
- console.error(e);
- return callback('Unable to parse username: ' + username);
- }
- body = {
- account_number: account_number,
- password: password
- };
- }
- console.log(IDP_ENDPOINT);
- var options = {
- method: 'POST',
- url: IDP_ENDPOINT,
- headers: {
- 'cache-control': 'no-cache',
- 'content-type': 'application/json'
- },
- body: body,
- json: true
- };
- request(options, function (error, response, body) {
- if (error) {
- throw new Error(error);
- }
- console.log('@@@@@@@>>>>');
- console.log(body);
- console.log('@@@@@@@<<<<');
- if (response.statusCode != 200 && response.statusCode != 201) {
- return callback(new Error('Wrong Username / Password'));
- }
- console.log(body);
- var user = body;
- // var user = JSON.parse(body);
- console.log('@@@ login - end @@@');
- callback(null, {
- user_id: user.id.toString(),
- nickname: user.nickname,
- email: user.email,
- email_verified: user.email_verified == 'true',
- //email_verified: true,
- user_metadata: {
- //account_number: user.account_number,
- googleId: user.google_id
- }
- });
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement