Authenticate clients in C# using Butterfly.Db and Butterfly.Web
Name | Package | Install |
---|---|---|
Butterfly.Auth | nuget install Butterfly.Auth |
git clone https://github.com/firesharkstudios/butterfly-auth
Normally, you will create a single instance of AuthManager. AuthManager only requires passing in an IDatabase instance; however, the following pattern is useful to get an AuthManager that verifies emails, verifies phone numbers, sends welcome emails, sends forgot password emails, etc.
var database = (initialize an IDatabase instance here)
var sendMessageQueueManager = (initialize SendMessageQueueManager here)
var welcomeEmailSendMessage = (load welcome email here)
var resetEmailSendMessage = (load reset email here)
var authManager = new AuthManager(
database,
onEmailVerify: sendMessageQueueManager.VerifyAsync,
onPhoneVerify: sendMessageQueueManager.VerifyAsync,
onRegister: user => {
sendMessageQueueManager.Queue(welcomeEmailSendMessage.Evaluate(user));
},
onForgotPassword: user => {
sendMessageQueueManager.Queue(resetEmailSendMessage.Evaluate(user));
}
);
See Butterfly.Db and Butterfly.Message for more information.
Butterfly.Auth requires Butterfly.Db to manage authentication tokens, users, and accounts.
While you can use any database engine supported by Butterfly.Db, here is the SQL to create the necessary tables in MySQL...
CREATE TABLE account (
id VARCHAR(50) NOT NULL,
created_at INT NOT NULL,
updated_at INT NOT NULL,
PRIMARY KEY(id)
);
CREATE TABLE user(
id VARCHAR(50) NOT NULL,
account_id VARCHAR(50) NOT NULL,
username VARCHAR(40) NOT NULL,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
email_verified_at INT NULL,
phone VARCHAR(20) NULL,
phone_verified_at INT NULL,
salt VARCHAR(40) NOT NULL,
password_hash VARCHAR(90) NOT NULL,
reset_code VARCHAR(6) NULL,
reset_code_expires_at INT NULL,
created_at INT NOT NULL,
updated_at INT NOT NULL,
PRIMARY KEY(id),
UNIQUE INDEX username(username)
);
CREATE TABLE auth_token(
id VARCHAR(50) NOT NULL,
user_id VARCHAR(50) NOT NULL,
expires_at INT NOT NULL,
created_at INT NOT NULL,
PRIMARY KEY(id)
);
Calling AuthManager.SetupWebApi() creates a web API that allows clients to make the following requests...
Returns true/false if the username is available
Returns true/false if the previously issues user ref token is valid
Creates an anonymous user (no username) assigning the user random first and last names
Registers a user (requires email, phone, and password fields)
Returns a valid user ref token
Logs in a user (requires username and password fields)
Returns a valid user ref token
Invokes the forgotPasswordAsync function passed to the constructor of AuthManager (normally sends a reset password email)
Resets a user's password
Invokes the forgotUsernameAsync function passed to the constructor of AuthManager (normally sends a forgot username email)
Invokes the verifyAsync function passed to the constructor of AuthManager (normally sends a verify email)
Invokes the verifyAsync function passed to the constructor of AuthManager (normally sends a verify text)
If you'd like to contribute, please fork the repository and use a feature branch. Pull requests are warmly welcome.
The code is licensed under the Mozilla Public License 2.0.