Class: PasskeyAuth::MagicLinksController

Inherits:
ApplicationController show all
Defined in:
app/controllers/passkey_auth/magic_links_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/passkey_auth/magic_links_controller.rb', line 17

def create
  email = params[:email]&.downcase&.strip
  user = PasskeyAuth.user_class.find_by(email: email)

  if user
    result = user.send_magic_link!
  else
    result = { success: false, error: "user_not_found" }
  end

  if result[:success]
    session[:passkey_auth_magic_link_email] = email
    redirect_to verify_code_magic_links_path
  else
    handle_error(result[:error])
  end
end

#newObject



13
14
15
# File 'app/controllers/passkey_auth/magic_links_controller.rb', line 13

def new
  @email = params[:email]
end

#showObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/passkey_auth/magic_links_controller.rb', line 35

def show
  link = MagicLink.consume_by_token(params[:token], ip: request.ip, user_agent: request.user_agent)

  if link
    user = link.user

    # Clear any previous session values
    if current_user && current_user.id != user.id
      reset_session
    end

    # Log the user in
    start_new_session_for(user)

    # If user has no passkeys, suggest setting one up after login
    if user.webauthn_credentials.none?
      session[:suggest_passkey_setup] = true
      redirect_to after_authentication_url, notice: I18n.t("passkey_auth.magic_links.success_with_passkey_prompt")
    else
      redirect_to after_authentication_url, notice: I18n.t("passkey_auth.magic_links.success")
    end
  else
    redirect_to new_session_path, alert: error_message(link)
  end
end

#verify_codeObject



61
62
63
64
# File 'app/controllers/passkey_auth/magic_links_controller.rb', line 61

def verify_code
  @email = session[:passkey_auth_magic_link_email]
  redirect_to new_magic_link_path if @email.blank?
end

#verify_with_codeObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/passkey_auth/magic_links_controller.rb', line 66

def verify_with_code
  email = params[:email]&.downcase&.strip
  short_code = MagicLink::ShortCode.clean(params[:code])

  link = MagicLink.consume_by_email_and_code(email, short_code, ip: request.ip, user_agent: request.user_agent)

  if link
    user = link.user

    # Clear the session email
    session.delete(:passkey_auth_magic_link_email)

    # Log the user in
    start_new_session_for(user)

    # If user has no passkeys, suggest setting one up after login
    if user.webauthn_credentials.none?
      session[:suggest_passkey_setup] = true
      redirect_to after_authentication_url, notice: I18n.t("passkey_auth.magic_links.success_with_passkey_prompt")
    else
      redirect_to after_authentication_url, notice: I18n.t("passkey_auth.magic_links.success")
    end
  else
    flash.now[:alert] = error_message(link)
    @email = email
    render :verify_code, status: :unprocessable_content
  end
end