### Install OAuth Plugins Source: https://limenauth.dev/blog/introducing-limen Install the necessary OAuth plugins for Limen using go get. ```go go get github.com/thecodearcher/limen/plugins/oauth go get github.com/thecodearcher/limen/plugins/oauth-google ``` -------------------------------- ### Install Limen and Dependencies Source: https://limenauth.dev/blog/introducing-limen Install the Limen core library, the SQL adapter, and the credential-password plugin. Ensure you have Go 1.25+ installed. ```go go get github.com/thecodearcher/limen go get github.com/thecodearcher/limen/adapters/sql go get github.com/thecodearcher/limen/plugins/credential-password ``` -------------------------------- ### Initialize Limen with Base URL and Password Plugin Source: https://limenauth.dev/ This example demonstrates initializing Limen with a specific BaseURL, a database adapter, and the password credential plugin. It's useful for setting up authentication in a client-server environment where the base URL is known. ```go package main import (...) func main() { auth, err := limen.New(&limen.Config{ BaseURL: "http://localhost:8080", Database: adapter.New(db), Plugins: []limen.Plugin{ credentialpassword.New(), }, }) if err != nil { log.Fatal(err) } mux := http.NewServeMux() mux.Handle("/api/auth/", auth.Handler()) } ``` -------------------------------- ### Install Limen Dependencies Source: https://limenauth.dev/docs/installation Add the core Limen library, a database adapter (GORM in this example), and the credential-password authentication plugin to your project. ```bash go get github.com/thecodearcher/limen go get github.com/thecodearcher/limen/adapters/gorm go get github.com/thecodearcher/limen/plugins/credential-password go get gorm.io/gorm gorm.io/driver/postgres ``` -------------------------------- ### Install Two-Factor Authentication Plugin Source: https://limenauth.dev/blog/introducing-limen Install the two-factor authentication plugin for Limen using go get. ```go go get github.com/thecodearcher/limen/plugins/two-factor ``` -------------------------------- ### Run Limen Application Source: https://limenauth.dev/docs/installation Start your Go application to begin using Limen for user authentication. ```bash go run main.go ``` -------------------------------- ### Wire Up Limen Core with PostgreSQL Source: https://limenauth.dev/blog/introducing-limen Set up the main Go application to initialize Limen with a PostgreSQL database adapter and the credential-password plugin. This example demonstrates basic configuration including database connection, session secret, and plugin registration. ```go package main import ( "database/sql" "log" "net/http" "os" _ "github.com/lib/pq" "github.com/thecodearcher/limen" sqladapter "github.com/thecodearcher/limen/adapters/sql" credentialpassword "github.com/thecodearcher/limen/plugins/credential-password" ) func main() { db, err := sql.Open("postgres", os.Getenv("DATABASE_URL")) if err != nil { log.Fatal(err) } defer db.Close() auth, err := limen.New(&limen.Config{ BaseURL: "http://localhost:8080", Database: sqladapter.NewPostgreSQL(db), Secret: []byte(os.Getenv("LIMEN_SECRET")), // 32 bytes Plugins: []limen.Plugin{ credentialpassword.New(), }, }) if err != nil { log.Fatal(err) } mux := http.NewServeMux() mux.Handle("/api/auth/", auth.Handler()) log.Println("listening on :8080") log.Fatal(http.ListenAndServe(":8080", mux)) } ``` -------------------------------- ### Create Limen Instance and Get Handler Source: https://limenauth.dev/docs/installation Instantiate Limen with the configuration and retrieve the HTTP handler to be mounted in your server. ```go auth, err := limen.New(config) if err != nil { log.Fatalf("Failed to create limen: %v", err) } handler := auth.Handler() ``` -------------------------------- ### Add Custom User Fields with Limen Schema System Source: https://limenauth.dev/blog/introducing-limen Extend the user schema in Limen to include custom fields like 'display_name' without forking the core. This example shows how to define a required 'display_name' field. ```go limen.WithSchemaUser( limen.WithUserAdditionalFields(func(ctx *limen.AdditionalFieldsContext) (map[string]any, error) { if ctx.IsEmpty("display_name") { return nil, limen.NewLimenError("display_name is required", http.StatusBadRequest, nil) } return map[string]any{ "display_name": ctx.GetBodyValue("display_name"), }, }), ) ``` -------------------------------- ### Initialize GORM Database Adapter Source: https://limenauth.dev/docs/installation Open a database connection using GORM and wrap it with the Limen GORM adapter to enable database interactions. ```go package main import ( "log" gormadapter "github.com/thecodearcher/limen/adapters/gorm" "gorm.io/driver/postgres" "gorm.io/gorm" ) func main() { dsn := "host=localhost user=postgres password=postgres dbname=myapp port=5432 sslmode=disable" gormdb, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) if err != nil { log.Fatalf("Failed to open database: %v", err) } adapter := gormadapter.New(gormdb) } ``` -------------------------------- ### Configure Limen with Plugins Source: https://limenauth.dev/docs/installation Build the limen.Config object, specifying the base URL, database adapter, and desired authentication plugins like credential-password. ```go import ( "github.com/thecodearcher/limen" credentialpassword "github.com/thecodearcher/limen/plugins/credential-password" ) config := &limen.Config{ BaseURL: "http://localhost:8080", Database: adapter, Plugins: []limen.Plugin{ credentialpassword.New(), }, } ``` -------------------------------- ### Initialize Limen Authentication with Password Plugin Source: https://limenauth.dev/ This snippet shows how to initialize Limen with a configuration that includes a database adapter and the password credential plugin. It then sets up a basic HTTP handler for authentication routes. ```go package main func main() { auth, err := limen.New( &limen.Config{ Database: adapter.New(db), Plugins: []limen.Plugin{ credentialpassword.New(), } } ) if err != nil { log.Fatal(err) } mux := http.NewServeMux() mux.Handle("/api/auth/", auth.Handler()) } ``` -------------------------------- ### Enable Limen CLI for Migrations Source: https://limenauth.dev/docs/installation Add the CLI configuration to your Limen config to enable the CLI for generating database migrations. ```go &limen.Config{ //... other config options ... CLI: &limen.CLIConfig{ Enabled: true, }, //... other config options ... } ``` -------------------------------- ### Set Up Environment Variable for Secret Key Source: https://limenauth.dev/docs/installation Create a .env file and add the LIMEN_SECRET variable with a 32-character signing secret for encryption. ```env LIMEN_SECRET=your-32-byte-signing-secret-here! ``` -------------------------------- ### Generate Database Migrations with Limen CLI Source: https://limenauth.dev/docs/installation Use the Limen CLI to generate SQL migration files based on your database schema and driver. ```bash limen generate migrations --driver postgres --dsn "host=localhost user=postgres password=postgres dbname=myapp port=5432 sslmode=disable" ``` -------------------------------- ### User Signup with Credential Password Source: https://limenauth.dev/blog/introducing-limen This snippet shows the JSON payload for signing up a user using the credential-password endpoint. It requires the email and password for the new user. ```json { "email": "ada@example.com", "password": "correct-horse-battery" } ``` -------------------------------- ### Configure Google OAuth in Limen Source: https://limenauth.dev/blog/introducing-limen Integrate Google OAuth into your Limen application by adding the oauth-google plugin. Ensure GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET are set in your environment or passed explicitly. ```go import ( "github.com/thecodearcher/limen/plugins/oauth" oauthgoogle "github.com/thecodearcher/limen/plugins/oauth-google" ) auth, _ := limen.New(&limen.Config{ // ... BaseURL, Database, Secret ... Plugins: []limen.Plugin{ credentialpassword.New(), oauth.New(oauth.WithProviders( oauthgoogle.New(), )), }, }) ``` -------------------------------- ### Protect a Route with Limen Session Source: https://limenauth.dev/blog/introducing-limen Demonstrates how to retrieve and use the user session to protect an API route. The `auth.GetSession(r)` method can be used in any handler or middleware that has access to the `http.Request`. ```go mux.HandleFunc("GET /api/me", func(w http.ResponseWriter, r *http.Request) { session, err := auth.GetSession(r) if err != nil || session == nil { http.Error(w, "Unauthorized", http.StatusUnauthorized) return } // session.User and session.Session are fully typed json.NewEncoder(w).Encode(session.User) }) ``` -------------------------------- ### Enable Two-Factor Authentication in Limen Source: https://limenauth.dev/blog/introducing-limen Add the two-factor plugin to your Limen configuration to enable TOTP authenticator enrollment and backup code generation for users. ```go import twofactor "github.com/thecodearcher/limen/plugins/two-factor" Plugins: []limen.Plugin{ credentialpassword.New(), oauth.New( /* ... */ ), twofactor.New(), }, ``` -------------------------------- ### User Signin with Credential Password Source: https://limenauth.dev/blog/introducing-limen This snippet shows the JSON payload for signing in a user using the credential-password endpoint. It requires the user's email or username and their password. ```json { "credential": "ada@example.com", "password": "correct-horse-battery" } ``` -------------------------------- ### Mount Limen Handler in HTTP Server Source: https://limenauth.dev/docs/installation Register the Limen HTTP handler with your HTTP multiplexer, typically under an authentication-related path. ```go mux := http.NewServeMux() mux.Handle("/auth/", handler) log.Fatal(http.ListenAndServe(":8080", mux)) ``` -------------------------------- ### Limen OAuth Endpoints Source: https://limenauth.dev/blog/introducing-limen Limen automatically exposes API endpoints for OAuth flows. These endpoints handle authorization, callback, token exchange, and user profile fetching. ```text GET /api/auth/oauth/google/authorize?callback_url=https://yourapp.com/callback GET /api/auth/oauth/google/callback ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.