Created
June 3, 2017 10:24
-
-
Save Gowiem/72b5c20ef45a6df82339569dc2917131 to your computer and use it in GitHub Desktop.
DeviseInvitable + Rails API + Ember Code Blocks
This file contains 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
// 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'); | |
}); | |
} | |
} | |
}); |
This file contains 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
// 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; |
This file contains 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
# backend/config/routes.rb | |
Rails.application.routes.draw do | |
devise_for :users, controllers: { invitations: 'users_invitations' } | |
# ... | |
root to: 'application#root' | |
end |
This file contains 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
# backend/app/controllers/users_invitations_controller.rb | |
class UsersInvitationsController < ApplicationController | |
end |
This file contains 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
# 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 |
This file contains 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
# 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