User Signup

User Signup

This example shows how you can implement user signup on the server side using the DID Token.

The example assumes:

  • You have already configured your client-side app with the Magic Client SDK
  • The example below is assuming you are already using a Ruby Web Framework (Rails, Sinatra, etc.) Web framework's specific imports are omitted in favor of the simplicity of the example. Only the magic_admin related imports are shown below

You will only need to handle the DID Token. No more password handling โœจ

important

It is important to always validate the DID Token before using.

require 'magic-admin'
require 'magic_user_signup_service'
# Using
MagicUserSignupService.call(headers, name, email)
# Definition
class MagicUserSignupService
def self.call(headers, name, email)
new(headers).signup(name, email)
end
def initialize(headers = {})
@headers = headers
@magic = Magic.new(api_secret_key: '<YOUR_API_SECRET_KEY>')
end
def signup(name, email)
metadata = get_metadata
# Handle the user mismatch by raising your application error. ``UnauthorizedError``
# is an example.
raise UnauthorizedError.new('UnAuthorized user signup') unless metadata.data[:data][:email] == email
# Application logics to create and sign up the new user.
end
def get_metadata
begin
validate_did_token?
magic.user.get_metadata_by_issuer(issuer)
rescue MagicAdmin::DIDTokenError => e
e.message
# Your rescue code
rescue MagicAdmin::RequestError => e
e.message
# Your rescue code
end
end
private
attr_reader :headers, :magic
def issuer
magic.token.get_issuer(did_token)
end
def validate_did_token?
magic.token.validate(did_token)
end
def did_token
return nil if headers['Authorization'].nil?
headers['Authorization'].split(' ').last
end
end