Module: BetterAuth::Passkey::Routes::Management

Defined in:
lib/better_auth/passkey/routes/management.rb

Class Method Summary collapse

Class Method Details

.delete_passkey_endpointObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/better_auth/passkey/routes/management.rb', line 17

def delete_passkey_endpoint
  Endpoint.new(path: "/passkey/delete-passkey", method: "POST", metadata: Routes.openapi_for(:delete_passkey)) do |ctx|
    session = BetterAuth::Routes.current_session(ctx)
    body = Utils.normalize_hash(ctx.body)
    Utils.require_string!(body, :id)
    passkey = ctx.context.adapter.find_one(
      model: "passkey",
      where: [
        {field: "id", value: body[:id]},
        {field: "userId", value: session.fetch(:user).fetch("id")}
      ]
    )
    raise APIError.new("NOT_FOUND", message: ErrorCodes::PASSKEY_ERROR_CODES.fetch("PASSKEY_NOT_FOUND")) unless passkey

    deleted = ctx.context.adapter.delete_many(
      model: "passkey",
      where: [
        {field: "id", value: passkey.fetch("id")},
        {field: "userId", value: session.fetch(:user).fetch("id")}
      ]
    )
    raise APIError.new("NOT_FOUND", message: ErrorCodes::PASSKEY_ERROR_CODES.fetch("PASSKEY_NOT_FOUND")) if deleted.to_i.zero?

    ctx.json({status: true})
  end
end

.list_passkeys_endpointObject



9
10
11
12
13
14
15
# File 'lib/better_auth/passkey/routes/management.rb', line 9

def list_passkeys_endpoint
  Endpoint.new(path: "/passkey/list-user-passkeys", method: "GET", metadata: Routes.openapi_for(:list_passkeys)) do |ctx|
    session = BetterAuth::Routes.current_session(ctx)
    passkeys = ctx.context.adapter.find_many(model: "passkey", where: [{field: "userId", value: session.fetch(:user).fetch("id")}])
    ctx.json(passkeys.map { |passkey| Credentials.wire(passkey) })
  end
end

.update_passkey_endpointObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/better_auth/passkey/routes/management.rb', line 44

def update_passkey_endpoint
  Endpoint.new(path: "/passkey/update-passkey", method: "POST", metadata: Routes.openapi_for(:update_passkey)) do |ctx|
    session = BetterAuth::Routes.current_session(ctx)
    body = Utils.normalize_hash(ctx.body)
    Utils.require_string!(body, :id)
    unless body.key?(:name) && body[:name].is_a?(String)
      raise APIError.new("BAD_REQUEST", message: BASE_ERROR_CODES.fetch("VALIDATION_ERROR"))
    end

    passkey = ctx.context.adapter.find_one(
      model: "passkey",
      where: [
        {field: "id", value: body[:id]},
        {field: "userId", value: session.fetch(:user).fetch("id")}
      ]
    )
    raise APIError.new("NOT_FOUND", message: ErrorCodes::PASSKEY_ERROR_CODES.fetch("PASSKEY_NOT_FOUND")) unless passkey

    updated = ctx.context.adapter.update(
      model: "passkey",
      where: [
        {field: "id", value: body[:id]},
        {field: "userId", value: session.fetch(:user).fetch("id")}
      ],
      update: {name: body[:name].to_s}
    )
    raise APIError.new("NOT_FOUND", message: ErrorCodes::PASSKEY_ERROR_CODES.fetch("PASSKEY_NOT_FOUND")) unless updated

    ctx.json({passkey: Credentials.wire(updated)})
  end
end