Created
July 7, 2020 19:21
-
-
Save kitsuneyo/ceb3b30b444a2787a257ac9f4fbe810b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Controller from '@ember/controller'; | |
import { inject as service } from '@ember/service'; | |
import { action } from "@ember/object"; | |
import { tracked } from "@glimmer/tracking"; | |
export default class LoginController extends Controller { | |
@tracked errorMessage; | |
@service session; | |
identification = '[email protected]'; | |
password = 'password'; | |
@action | |
authenticate() { | |
let { identification, password } = this; | |
return this.session.authenticate( | |
'authenticator:my', identification, password | |
) | |
.then(() => { | |
this.transitionTo('index'); | |
}) | |
.catch(e => { | |
if (e.errors) { | |
log(`Error: ${e.errors[0].title}`); | |
return this.set('errorMessage', e.errors[0]); | |
} | |
return reject(e); | |
}); | |
} | |
} |
This file contains hidden or 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
import DeviseAuthenticator from 'ember-simple-auth/authenticators/devise'; | |
import ENV from 'id/config/environment'; | |
import { isEmpty } from '@ember/utils'; | |
import { merge, assign as emberAssign } from '@ember/polyfills'; | |
import fetch from 'fetch'; | |
const assign = emberAssign || merge; | |
export default class MyAuthenticator extends DeviseAuthenticator { | |
serverTokenEndpoint = `${ENV.APP.hosts.api}/user_authentications/`; | |
resourceName = 'attributes'; | |
tokenAttributeName = 'jwt'; | |
// authenticate(identification, password) { | |
// return new Promise((resolve, reject) => { | |
// const { resourceName, identificationAttributeName, tokenAttributeName } = this.getProperties('resourceName', 'identificationAttributeName', 'tokenAttributeName'); | |
// const data = {}; | |
// data[resourceName] = { password }; | |
// data[resourceName][identificationAttributeName] = identification; | |
// | |
// this.makeRequest(data).then((response) => { | |
// console.log('hi'); | |
// if (response.ok) { | |
// response.json().then((json) => { | |
// if (this._validate(json)) { | |
// run(null, resolve, json); | |
// } else { | |
// run(null, reject, `Check that server response includes ${tokenAttributeName} and ${identificationAttributeName}`); | |
// } | |
// }); | |
// } else { | |
// run(null, reject, response); | |
// } | |
// }).catch((error) => run(null, reject, error)); | |
// }); | |
// } | |
makeRequest(data, options = {}) { | |
let url = options.url || this.get('serverTokenEndpoint'); | |
let requestOptions = {}; | |
let body = JSON.stringify( | |
{ data: { type: "user_authentications", attributes: data['attributes'] } } | |
); | |
assign(requestOptions, { | |
body, | |
method: 'POST', | |
headers: { | |
'Accept': ENV.APP.jsonContentType, | |
'Content-Type': ENV.APP.jsonContentType | |
} | |
}); | |
assign(requestOptions, options || {}); | |
return fetch(url, requestOptions); | |
} | |
_validate(data) { | |
const _data = data['data']['attributes'] ? data['data']['attributes'] : data; | |
return !isEmpty(_data['jwt']) && !isEmpty(_data['user_id']); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment