lundi 18 décembre 2017

EmberJS - Ember Simple Auth Oath2 'grant _type' not defined

I've been following the Ember Simple Auth walkthrough available here. I have added the various code snippets as instructed but when I submit my login form I receive a 'grant_type' not defined error.

Here is the current setup:

// Login Form
        <form >
          <label for="identification">Login</label> 
          <br>
          <label for="password">Password</label> 
          <br>
          <button type="submit" class="btn btn-default">Login</button>
        </form>

        
          <p>
            <strong>Login failed: </strong>
            <code></code>
          </p>
        

//index.js controller

import Controller from '@ember/controller';

export default Controller.extend({
  session: Ember.inject.service('session'),

  actions: {
    invalidateSession() {
      this.get('session').invalidate();
    },
    authenticate() {
      let {identification, password } = this.getProperties('identification', 'password');
      this.get('session').authenticate('authenticator:oath2', identification, password).catch((reason) => {this.set('errorMessage', reason.error)
    })
    }
  }

});

//application route

import Route from '@ember/routing/route';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';

export default Route.extend({

});

// authenticators/oath.js

import OAuth2PasswordGrantAuthenticator from 'ember-simple-auth/authenticators/oauth2-password-grant';

export default OAuth2PasswordGrantAuthenticator.extend({
  serverTokenEndpoint: 'http://server:port/api/token',


});

//api endpoint

var tokenRouter = express.Router();

tokenRouter.post('/api/token', function(req, res) {


  if (req.body.grant_type === 'password') {
    if (req.body.username === 'letme' && req.body.password === 'in') {
      res.status(200).send('{"access_token": "secret token!"}');
    } else {
      res.status(400).send('{ "error": invalid_grant_type" }')
    }
    } else {
      res.status(400).send(' { "error": "unsupported_grant_type" }')
    }

})

app.use('/', tokenRouter)

The request is successfully sent to my endpoint and the 500 error is generated with the message that grant_type is not defined. Looking at the request, it doesn't look like the username or password are being sent either.

As far as I can tell my code is identical to the code from the documentation and supplementary video but I am obviously missing something.




Aucun commentaire:

Enregistrer un commentaire