Devise-web3 allows to easily implement login with metamask or any other ethereum wallet providers. It works by providing a nonce for a user to sign and then cryptographically checking that signature is valid. This allows for a simple one click login popular among web3 apps.
Devise-web3adds a new controller that generates anoncefor a wallet provider to sign(eg. metamask) along withnonce_idnoncegets signed by a wallet providersignature,nonce_idand walletaddressis then sent by frontend to a regular sign_in endpoint provided by deviseDevise-web3then checks the signature and authenticates user
Add this line to your application's Gemfile:
gem 'devise-web3'And then execute:
$ bundle install
Or install it yourself as:
$ gem install devise-web3
Add :web3_authenticatable module to your model along your other devise modules
Example
class User < ApplicationRecord
devise :jwt_authenticatable,
:web3_authenticatable
end
Generate migration
bin/rails g active_record:devise_web3 usersthen run the migration
Generate config
bin/rails devise:web3:installProvide your redis url in config
config.web3 do |web3|
web3.redis_url = nil
endYou're all set. It's time to setup a frontend
For vue you can use a vue component or simply check the source code to see the general flow
- Fetch user wallet address
const accounts = await window.ethereum.request({ method: "eth_requestAccounts" })
const address = accounts[0]
- Fetch
nonceandnonce_idthrough API - Sign the nonce
const web3 = new Web3()
const signature = await ethereum.request({
method: "personal_sign",
params: [web3.utils.fromUtf8(nonce), address],
})
- Send
{ credenatils: { signature: signature, nonce_id: nonce_id, address: address }}to sign in endpoint
There are many possibible improvements to Devise-web3. Feel free to create an issue with your requests
The gem is available as open source under the terms of the MIT License.