Class: Remotty::Users::SessionsController

Inherits:
Devise::SessionsController
  • Object
show all
Includes:
ActionController::Flash, BaseController
Defined in:
app/controllers/remotty/users/sessions_controller.rb

Instance Method Summary collapse

Instance Method Details

#create {|resource| ... } ⇒ Object

POST /resource/sign_in email과 password로 로그인 새로운 토큰 생성

return

  • success - 로그인 후 user with token json return

  • failure - unauthorized with error message

Yields:

  • (resource)


15
16
17
18
19
20
21
# File 'app/controllers/remotty/users/sessions_controller.rb', line 15

def create
  self.resource = warden.authenticate!(:scope => resource_name)
  (resource_name, resource, store: false)
  yield resource if block_given?
  token = resource.generate_auth_token!(auth_source)
  render json: resource.with_token(token)
end

#destroy {|resource| ... } ⇒ Object

DELETE /resource/sign_out 로그아웃. 로그인이 되어 있지 않아도 에러를 발생하지는 않음 토큰이용시 토큰을 삭제함

return

  • success - no_content

  • failure - no_content

Yields:

  • (resource)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/remotty/users/sessions_controller.rb', line 31

def destroy
  user = current_user

  signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name))
  if signed_out
    if user && request.headers["X-Auth-Token"].present?
      auth_token = user.auth_tokens.where(token: Digest::SHA512.hexdigest(request.headers["X-Auth-Token"])).first
      auth_token.destroy if auth_token
    end

    session.options[:skip] = true
    response.headers['Set-Cookie'] = 'rack.session=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT'
  end
  yield resource if block_given?

  render nothing: true, status: :no_content
end

#showObject

GET /resource 로그인한 사용자 정보 가져오기

  • success - current_user json return

  • failure - unauthentication with error message



54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/remotty/users/sessions_controller.rb', line 54

def show
  resource = warden.authenticate(:scope => resource_name)
  if resource
    render json: resource
  else
    render json: {
      error: {
        code: "UNAUTHENTICATION"
      }
    }, :status => :unauthorized
  end
end