Skip to content
blog.fossasia.org
  • Home
  • Projects
    • Contribute
  • Events
    • Eventyay Platform
    • Event Sponsorships
    • Event Calendar
    • FOSSASIA Summit
    • OpenTechSummit China
    • OpenTechSummit Thailand
    • OpenTechSummit Vietnam
    • Jugaad Fest India
    • Past Events
      • FOSSASIA Summit 2022
      • FOSSASIA Summit 2021
      • FOSSASIA Summit 2020
      • FOSSASIA Summit 2019
      • FOSSASIA Summit 2018
      • FOSSASIA Summit 2017
      • FOSSASIA Summit 2016
      • FOSSASIA Summit 2015
      • FOSSASIA Summit 2014
      • FOSSASIA Summit 2012
      • FOSSASIA Summit 2011
      • FOSSASIA Summit 2010
      • GNOME.Asia 2009
      • MiniDebConf Vietnam 2010
      • Sciencehack.Asia
      • Science Hack India
  • Programs
    • Programs and Opportunities
    • Jobs Opportunities
    • Program Guidelines
    • Codeheat Contest
    • University Internship Program
    • University Student Coding Programs
    • High School Student Program
    • Advanced Developer Program
    • Become a Mentor
      • Become A University Student Mentor
      • Become A High School Student Mentor
  • Shop
  • Blog
  • About
    • Jobs
    • Membership
    • Activities
    • Background & Mission
    • Best Practices
    • Licenses
    • Team
    • Code of Conduct
  • Donate
Menu Close
  • Home
  • Projects
    • Contribute
  • Events
    • Eventyay Platform
    • Event Sponsorships
    • Event Calendar
    • FOSSASIA Summit
    • OpenTechSummit China
    • OpenTechSummit Thailand
    • OpenTechSummit Vietnam
    • Jugaad Fest India
    • Past Events
      • FOSSASIA Summit 2022
      • FOSSASIA Summit 2021
      • FOSSASIA Summit 2020
      • FOSSASIA Summit 2019
      • FOSSASIA Summit 2018
      • FOSSASIA Summit 2017
      • FOSSASIA Summit 2016
      • FOSSASIA Summit 2015
      • FOSSASIA Summit 2014
      • FOSSASIA Summit 2012
      • FOSSASIA Summit 2011
      • FOSSASIA Summit 2010
      • GNOME.Asia 2009
      • MiniDebConf Vietnam 2010
      • Sciencehack.Asia
      • Science Hack India
  • Programs
    • Programs and Opportunities
    • Jobs Opportunities
    • Program Guidelines
    • Codeheat Contest
    • University Internship Program
    • University Student Coding Programs
    • High School Student Program
    • Advanced Developer Program
    • Become a Mentor
      • Become A University Student Mentor
      • Become A High School Student Mentor
  • Shop
  • Blog
  • About
    • Jobs
    • Membership
    • Activities
    • Background & Mission
    • Best Practices
    • Licenses
    • Team
    • Code of Conduct
  • Donate

ember simple auth

Read more about the article How Authentication works in the Ember JS Open Event Frontend

How Authentication works in the Ember JS Open Event Frontend

  • Post author:CosmicCoder96
  • Post published:July 14, 2017
  • Post category:FOSSASIA/Open Event
  • Post comments:0 Comments

After decoupling of the frontend and backend where in Open Event Orga Server has been transformed into  JSON:API Spec Compliant REST API , the front end resides in Open Event Frontend, and this blog post illustrates how the login and authentication works on Open Event Frontend using the new API.

First, we will briefly explore the UI of the login form, it’s a standard login form, with the ability to display errors in case something goes wrong. Like all forms it is stored in it’s separate component. With two fields an email and a password. Brief discussion of the UI is important since the field names will be the same ones used for fetching values making requests to the API. In case there is an error, for instance wrong credentials, it gets passed to the errorMessage property of the component. For entire code for the UI of the form, please refer to the Resources section at the end  of the blog. The only relevant details which we need from the UI are the names of the fields, which are email and password respectively.And that the login button triggers the submit action of the form.

So we begin our discussion with a brief description of the package which manages the authentication in Open Event Front End. ember-simple-auth  has been used to manage all the authentication needs, including maintaining the current user session. To quote it’s official documentation it helps us achieve the following goals in our project among it’s many other capabilities which are not being used here :

  • it maintains a client side session and synchronises its state across multiple tabs/windows of the application
  • it authorises requests to backend servers

In addition to ember-simple-auth we make use of ember-simple-auth-token which is an extension to ember-simple-auth library. It provides the token authenticator and thus handles communicating with the backend to receive a token successfully.

Next we need to configure the above two libraries, to connect to our API located at open-event-api.herokuapp.com The documentation tells us exactly what routes we need, in our case we need to make a POST request to the route  /auth/session  and pass in email and password in the body. The Content-Type should be specified as application/json.   If everything goes well, we should receive a JWT access token in response.To configure the libraries, we specify the following in  config/environment.js file.

ENV['ember-simple-auth'] = {
authorizer: 'authorizer:jwt'
};
ENV['ember-simple-auth-token'] = {
refreshAccessTokens      : false,
serverTokenEndpoint      : `${ENV.APP.apiHost}/auth/session`,
identificationField      : 'email',
passwordField            : 'password',
tokenPropertyName        : 'access_token',
refreshTokenPropertyName : 'refresh_token',
authorizationPrefix      : 'JWT ',
authorizationHeaderName  : 'Authorization',
headers                  : {}
};

So we begin our discussion from the point where user has entered the credentials and presses the login button which triggers the submit action. Following is the code for the submit action and we will examine each section of it in this article.

submit() {
this.onValid(() => {
  let credentials = this.getProperties('identification', 'password'),
    authenticator = 'authenticator:jwt';

  this.set('errorMessage', null);
  this.set('isLoading', true);

  this.get('session')
    .authenticate(authenticator, credentials)
    .then(() => {
      const tokenPayload = this.get('authManager').getTokenPayload();
      if (tokenPayload) {
        this.get('store').findRecord('user', tokenPayload.identity).then(user => {
          this.get('session').set('data.currentUser', user);
        });
      }
    })
    .catch(reason => {
      if (reason.hasOwnProperty('status_code') && reason.status_code === 401) {
        this.set('errorMessage', this.l10n.t('Your credentials were incorrect.'));
      } else {
        this.set('errorMessage', this.l10n.t('An unexpected error occurred.'));
      }
      this.set('isLoading', false);
    })
  });
}

this.onValid()  ensures that the rest of the action is executed only if the values entered by the user pass the basic form validations. Using getProperties, the values entered by the user are stored in credentials. Next, the authenticator is specified and isLoading is set to true to show the loading icon while the request is processed. And then we use the authenticate method of ember simple auth  to validate the credentials. If the authentication is successful, the then clause executes and we fetch the correct  user from the model using tokenPayload which contains the required information in the form of it’s identity  property and set it as the current user for the session.

However if the authentication fails, the catch block executes.  The API is configured to return the correct status code 401, in case the credentials were wrong, in which case the errorMessage is set to the appropriate response. The else clause handles all other cases where authentication fails and gives a generic error message. Finally the isLoading  is set to false. And if the authentication was successful, the user is redirected to the index route else, stays on the same route with the error message.

 

Resources

  • Ember JS
  • Ember simple auth
  • Ember simple auth token
  • Open Event API Docs
  • Open Event Front End Login Form template
  • JSON:API Spec
Continue ReadingHow Authentication works in the Ember JS Open Event Frontend
  • FOSSASIA
  • Blog
  • GitHub
  • Projects
  • Code of Conduct
  • About
  • Contact
Copyright - OceanWP Theme by OceanWP
 

Loading Comments...
 

You must be logged in to post a comment.