Skip to content

Instantly share code, notes, and snippets.

@truongnmt
Created January 9, 2022 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save truongnmt/b2e73af945713abbd9cabbb0ad16c446 to your computer and use it in GitHub Desktop.
Save truongnmt/b2e73af945713abbd9cabbb0ad16c446 to your computer and use it in GitHub Desktop.
registrations_controller.rb
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