Skip to content

Instantly share code, notes, and snippets.

@Gowiem
Created June 3, 2017 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gowiem/72b5c20ef45a6df82339569dc2917131 to your computer and use it in GitHub Desktop.
Save Gowiem/72b5c20ef45a6df82339569dc2917131 to your computer and use it in GitHub Desktop.
DeviseInvitable + Rails API + Ember Code Blocks
{{!-- app/templates/accept-invitation.hbs --}}
{{#bs-form model=model onSubmit=(route-action "submit" model) as |f|}}
{{f.element controlType="password" label="Password" placeholder="Password" property="password" required=true}}
{{f.element controlType="password" label="Password Confirmation" placeholder="Password Confirmation" property="password_confirmation" required=true}}
{{bs-button defaultText="Submit" type="primary" buttonType="submit"}}
{{/bs-form}}
// frontend/app/routes/accept-invitation.js
import Ember from 'ember';
import config from '../config/environment';
const { inject: { service }, isEmpty } = Ember;
export default Ember.Route.extend({
session: service('session'),
beforeModel() {
if (this.get('session.isAuthenticated')) {
this.get('session').invalidate();
}
},
model() {
return Ember.Object.create({ password: null, password_confirmation: null, invitation_token: null });
},
afterModel(model, transition) {
let invitationToken = transition.queryParams.invitation_token;
if (isEmpty(invitationToken)) {
this.transitionTo('dashboard');
} else {
model.set('invitation_token', invitationToken);
}
},
actions: {
submit(model) {
let body = { user: model.getProperties('invitation_token', 'password', 'password_confirmation') };
Ember.$.ajax({
url: `${config.APP.API_HOST}/users/invitation`,
type: 'PUT',
data: body,
}).then(() => {
this.transitionTo('login');
});
}
}
});
// frontend/app/router.js
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: config.locationType,
rootURL: config.rootURL
});
Router.map(function() {
// ...
this.route('accept-invitation', { path: '/users/invitation/accept' });
});
export default Router;
# backend/config/routes.rb
Rails.application.routes.draw do
devise_for :users, controllers: { invitations: 'users_invitations' }
# ...
root to: 'application#root'
end
# backend/app/controllers/users_invitations_controller.rb
class UsersInvitationsController < ApplicationController
end
# backend/app/controllers/users_invitations_controller.rb
class UsersInvitationsController < Devise::InvitationsController
def edit
sign_out send("current_#{resource_name}") if send("#{resource_name}_signed_in?")
set_minimum_password_length
resource.invitation_token = params[:invitation_token]
redirect_to "http://localhost:8080/users/invitation/accept?invitation_token=#{params[:invitation_token]}"
end
def update
super do |resource|
if resource.errors.empty?
render json: { status: "Invitation Accepted!" }, status: 200 and return
else
render json: resource.errors, status: 401 and return
end
end
end
end
# backend/app/controllers/users_invitations_controller.rb
class UsersInvitationsController < Devise::InvitationsController
before_action :configure_permitted_parameters
# ...
private
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:accept_invitation, keys: [:first_name, :last_name])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment