ember-simple-auth-deviseとユーザ登録
追記
なんか正しく動いてなさげ。あとで直す、または別の方法を検討。
追記終わり
最近Ember.js関連のなにかを見ています。
Emberで認証をやろうとしたときにember-simple-authが便利です。バックエンドがRailsでDeviseを使うならば、ember-simple-auth-deviseがよいです。
チュートリアル的なやつとかで小さいサンプル作って遊んでたのですが、みんな頑なにユーザ登録のことに触れないのでソースちらちら見ると、ログイン・ログアウトは簡単に出来るのですが、どうやら登録は面倒見てくれないようです。
いろいろ調べたら出来たので、ごっそりメモ。
# front/app/controllers/signup.coffee `import Ember from 'ember'` `import { request } from 'ic-ajax'` SignupController = Ember.Controller.extend needs: ['login'] actions: signup: -> user = email: @get('identification') password: @get('password') password_confirmation: @get('password_confirmation') request '/users', type: 'POST' data: user: user dataType: 'json' .then (response) => if response['success'] console.log 'Signed up!' @get 'controllers.login' .get 'session' .authenticate 'simple-auth-authenticator:devise', identification: user['email'] password: user['password'] @transitionToRoute 'application' else console.log 'Oops!' `export default SignupController`
// front/app/templates/signup.emblem form submit='signup' = input id='identification' placeholder='Email' value=identification br = input id='password' placeholder='Password' type='password' value=password br = input id='password_confirmation' placeholder='Confirmation' type='password' value=password_confirmation br button type='submit' Sign up
# back/app/controller/sessions_controller.rb class SessionsController < Devise::SessionsController def create if self.resource = warden.authenticate(auth_options) if sign_in(resource_name, resource) return render json: { user_token: resource.authentication_token, user_email: resource.email } end end render json: {}, status: 401 end end
# back/app/controller/registrations_controller.rb class RegistrationsController < Devise::RegistrationsController def create build_resource devise_parameter_sanitizer.sanitize(:sign_up) if resource.save if resource.active_for_authentication? render json: {success: true} else expire_session_data_after_sign_in! render json: {success: true} end else clean_up_passwords resource render json: {success: false} end end end
# back/config/routes.rb devise_for :users, controllers: {sessions: 'sessions', registrations: 'registrations'}
# back/config/initializers/devise.rb config.http_authenticatable_on_xhr = false config.navigational_formats = ['*/*']
苦労したところの大半はDevise。そしてic-ajaxイケメン