Created
January 9, 2022 21:57
-
-
Save truongnmt/b2e73af945713abbd9cabbb0ad16c446 to your computer and use it in GitHub Desktop.
registrations_controller.rb
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
def callback | |
webauthn_credential = WebAuthn::Credential.from_create(params) | |
user = User.create!(session["current_registration"]["user_attributes"]) | |
begin | |
webauthn_credential.verify(session["current_registration"]["challenge"]) | |
credential = user.credentials.build( | |
external_id: Base64.strict_encode64(webauthn_credential.raw_id), | |
nickname: params[:credential_nickname], | |
public_key: webauthn_credential.public_key, | |
sign_count: webauthn_credential.sign_count | |
) | |
if credential.save | |
sign_in(user) | |
render json: { status: "ok" }, status: :ok | |
else | |
render json: "Couldn't register your Security Key", status: :unprocessable_entity | |
end | |
rescue WebAuthn::Error => e | |
render json: "Verification failed: #{e.message}", status: :unprocessable_entity | |
ensure | |
session.delete("current_registration") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment