ember new auth-example-frontend
β ember new auth-example-frontend installing app create .editorconfig create .ember-cli create .eslintrc.js create .travis.yml create .watchmanconfig create README.md create app/app.js create app/components/.gitkeep create app/controllers/.gitkeep create app/helpers/.gitkeep create app/index.html create app/models/.gitkeep create app/resolver.js create app/router.js create app/routes/.gitkeep create app/styles/app.css create app/templates/application.hbs create app/templates/components/.gitkeep create config/environment.js create config/targets.js create ember-cli-build.js create .gitignore create package.json create public/crossdomain.xml create public/robots.txt create testem.js create tests/.eslintrc.js create tests/helpers/destroy-app.js create tests/helpers/module-for-acceptance.js create tests/helpers/resolver.js create tests/helpers/start-app.js create tests/index.html create tests/integration/.gitkeep create tests/test-helper.js create tests/unit/.gitkeep create vendor/.gitkeep NPM: Installed dependencies
cd auth-example-frontend
ember-simple-auth
: ember install ember-simple-auth
β ember install ember-simple-auth NPM: Installed ember-simple-auth Installed addon package.
β ember g adapter application installing adapter create app/adapters/application.js installing adapter-test create tests/unit/adapters/application-test.js
// auth-example-frontend/app/adapters/application.js import DS from 'ember-data'; export default DS.JSONAPIAdapter.extend({ });
// auth-example-frontend/app/adapters/application.js import JSONAPIAdapter from 'ember-data/adapters/json-api'; import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin'; import config from 'my-app/config/environment'; export default JSONAPIAdapter.extend(DataAdapterMixin, { host: config.apiUrl, namespace: config.apiNamespace, authorizer: 'authorizer:application' });
JSONAPIAdapter
adapter, extended it using DataAdapterMixin
and added several properties. In particular, the values ββthat are written to the host
and namespace
are stored in the config/environment.js
file, which we also import. It has the following code responsible for the values ββof interest: let ENV = { ... apiNamespace: 'api', apiUrl: null };
apiNamespace
is the string api
, which affects the formation of the paths http://localhost:4000/api/users
. In this apiURL
we will set apiURL
using the proxy
option in ember-cli
, for example, like this: ember s --proxy http://localhost:4000
ember g authenticator oauth2 --base-class=oauth2
β ember g authenticator oauth2 --base-class=oauth2 installing authenticator create app/authenticators/oauth2.js
oauth2.js
file will be created: // auth-example-frontend/app/authenticators/oauth2.js import OAuth2PasswordGrant from 'ember-simple-auth/authenticators/oauth2-password-grant'; export default OAuth2PasswordGrant.extend({ });
oauth2.js
file: // auth-example-frontend/app/authenticators/oauth2.js import OAuth2PasswordGrant from 'ember-simple-auth/authenticators/oauth2-password-grant'; import config from 'my-app/config/environment'; const host = config.apiUrl || ''; const namespace = config.apiNamespace; const serverTokenEndpoint = [ host, namespace, 'token' ]; export default OAuth2PasswordGrant.extend({ serverTokenEndpoint: serverTokenEndpoint.join('/') });
serverTokenEndpoint
property we need by combining the host
and namespace
string constants with the string token
. As a result, for example, if we have the following: host = 'http://localhost:4000' namespace = 'api' token = 'token'
serverTokenEndpoint
will have the following string: http://localhost:4000/api/token
Authorization: Bearer s0m3tok3n1
ember-simple-auth
library makes it easy to set it up. We will use the OAuth2BearerAuthorizer class, since it, with little effort on our part, allows us to form the above header. ember g authorizer application --base-class=oauth2
β ember g authorizer application --base-class=oauth2 installing authorizer create app/authorizers/application.js
app/authorizers/application.js
: // auth-example-frontend/app/authorizers/application.js import OAuth2Bearer from 'ember-simple-auth/authorizers/oauth2-bearer'; export default OAuth2Bearer.extend({ });
ember g route sign-up
β ember g route sign-up installing route create app/routes/sign-up.js create app/templates/sign-up.hbs updating router add route sign-up installing route-test create tests/unit/routes/sign-up-test.js
sign-up.hbs
) and bring it to this form: <form {{action "onSignUp" email password on="submit"}}> <div> <label>Email</label> {{input type="email" value=email}} </div> <div> <label>Password</label> {{input type="password" value=password}} </div> <button type="submit">Sign Up</button> </form>
email
and password
fields. When a user clicks the Sign Up β
button Sign Up β
we onSignUp
action, passing him the entered email address and password. We define this action: // auth-example-frontend/routes/sign-up.js import Route from '@ember/routing/route'; import { inject as service } from '@ember/service'; const AUTHENTICATOR = 'authenticator:oauth2'; export default Route.extend({ session: service(), actions: { async onSignUp(email, password) { let attrs = { email, password }; let user = this.store.createRecord('user', attrs); await user.save(); let session = this.get('session'); await session.authenticate(AUTHENTICATOR, email, password); } } });
onSignUp
action to the route. If you are not familiar with how events work in Ember, then keep in mind that the onSignUp
action triggered in the template will involve the controller and then the route. Since we do not define an action in the controller, the route will process it.onSignUp
action onSignUp
we get email
and password
, so we use this data to create a user account. Then save this account and get the session service. The session service is part of ember-simple-auth
, here is its description:authenticator:oauth2
, as well as, as separate arguments, the password and email address.async/await
construct in your project, then it is quite possible to work without it. You can easily remove async/await
from the above code and use the then/catch/finally
to resolve promises. If you are interested in learning more about using async/await
in your projects, take a look at this material .user
model. If this is not done, we will encounter the following unpleasant error:user
model. It will contain only an email address and password. Create it by running the following generator: ember g model user email:string password:string
β ember g model user email:string password:string installing model create app/models/user.js installing model-test create tests/unit/models/user-test.js
user.js
model file created by this command should be user.js
to the following form: // auth-example-frontend/models/user.js import DS from 'ember-data'; export default DS.Model.extend({ email: DS.attr('string'), password: DS.attr('string') });
package.json
file and remove the following from it: "ember-welcome-page": "^3.0.0",
ember s
http://localhost:4200/sign-up
.Source: https://habr.com/ru/post/344384/
All Articles