moon add mizchi/js
moon add mizchi/npm_typed
npm install better-authimport {
"mizchi/js",
"mizchi/js_core" @core,
"mizchi/npm_typed/better_auth",
}# Generate migration files
npx @better-auth/cli@latest generate
# Apply migrations to database
npx @better-auth/cli@latest migratelet db = @sqlite.DatabaseSync::new("app.db")
let auth = @better_auth.betterAuth(
database=db |> @js.identity,
providers=[@better_auth.EmailPassword::{ enabled: true }],
secret="your-secret-key",
)
// Sign up
let response = auth.api().signUpEmail(
name="John Doe",
email="john@example.com",
password="password123",
)
if not(response.hasError()) {
let user = response.user()
let token = response.token()
// Set cookie: better-auth.session_token=token
}
// Sign in
let response = auth.api().signInEmail(
email="john@example.com",
password="password123",
)
if not(response.hasError()) {
let token = response.token()
// Set cookie: better-auth.session_token=token
}
// Get session
let session = auth.api().getSession(headers)
match session {
Some(s) => {
let user = s.user()
let session = s.session()
}
None => {
// Not authenticated
}
}#external
pub type Auth#external
pub type AuthApiasync fn AuthApi::signInEmail(self : AuthApi, email~ : String, password~ : String, remember_me? : Bool, callback_url? : String, headers? : Any) -> AuthResponseasync fn AuthApi::signUpEmail(self : AuthApi, name~ : String, email~ : String, password~ : String, image? : String, callback_url? : String) -> AuthResponse#external
pub type AuthClient#external
pub type AuthResponsepub(all) struct EmailPassword {
enabled : Bool
}impl Provider for EmailPasswordpub(all) struct GitHub {
client_id : String
client_secret : String
}pub(all) struct Google {
client_id : String
client_secret : String
}#external
pub type Session#external
pub type SessionResponse#external
pub type SignInClientasync fn SignInClient::email(self : SignInClient, email~ : String, password~ : String, remember_me? : Bool, callback_url? : String) -> AuthResponseasync fn SignInClient::social(self : SignInClient, provider~ : String, callback_url? : String) -> Any#external
pub type SignUpClientasync fn SignUpClient::email(self : SignUpClient, name~ : String, email~ : String, password~ : String, image? : String, callback_url? : String) -> AuthResponse#external
pub type UseSessionResult#external
pub type User