accounts

package module
v0.0.0-...-0d72348 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2024 License: MIT Imports: 12 Imported by: 0

README

Provides account registration & authentication endpoints for CL5.

Requirements

  • Go 1.23.2 or newer
  • A SQL driver and database.

Usage

The accounts server is a standard Fiber v2 app, and can be natively mounted.

package main

import (
    "database/sql"
    "github.com/gofiber/fiber/v2"
    "github.com/cloudlink-omega/accounts"
    "github.com/huandu/go-sqlbuilder"
    _ "github.com/ncruces/go-sqlite3/driver"
    _ "github.com/ncruces/go-sqlite3/embed"
)

func main() {

    // Initialize your database (SQLite as an example)
	db, err := sql.Open("sqlite3", "file:database.db")
	if err != nil {
		panic(err)
	}

    // . . .

    // Initialize a new Fiber app
    app := fiber.New()

    // . . . 

    // Initialize the Accounts service
    auth := accounts.New(
        "/",                     // router_path: The path that the server app will be mounted to. By default, leave this set to `/`.
        "http://localhost:3000", // server_url: The full hostname or URL that the server will be deployed to.
        "localhost",             // api_domain: The domain (or subdomain) that authorized cookies are permitted on.
        "localhost:3000",        // api_url: The interface that the Account server will listen to.
        "USA-OMEGA-1",           // server_name: Used for labeling the server. Recommended format: [Country Code]-[Server Nickname]-[Designation].
        "localhost:3000",        // primary_website: For use with pointing to any higher-level routers that may be mounted beyond the accounts server.
        "SESSION_KEY",           // session_key: Used for encrypting and decrypting JWT cookies. For example, use `openssl rand 60 | base64`.
        false,                   // enforce_https: Set to true if you are serving over HTTPS. This sets the "HTTPSOnly" value for cookies.
        db,                      // db: Use a database/sql compatible driver and insert an instance here. In this example, I am using SQLite.
		sqlbuilder.SQLite,       // db_flavor: See huandu/go-sqlbuilder flavors for your specific DB / driver.
		&types.MailConfig{       // email_config: Required for sending emails.
			Port:     587,
			Server:   "smtp.gmail.com",
			Username: "invalid@gmail.com",
			Password: " . . . ",
		},
    )

    // You can then configure OAuth providers. The currently supported ones are Google, Discord, and GitHub.

    /* Create a new Cloud App at https://console.cloud.google.com/projectcreate 
    * then go to the "APIs & Services" section. Select the "Credentials" category
    * and select "Create Credentials" > "OAuth client ID". Select "Web application"
    * as the Application type, and specify the Redirect URL under the "Authorized
    * redirect URLs" section ({server_url}/auth/google/callback). */
    auth.OAuth.Google("GOOGLE_KEY", "GOOGLE_SECRET")

    /* https://github.com/settings/developers > New OAuth App
    * Once you've created the application, you will need to specify
    * the Redirect URL under the "Authorization callback URL" section
    * ({server_url}/auth/github/callback). */
    auth.OAuth.GitHub("GITHUB_KEY", "GITHUB_SECRET")

    /* https://discord.com/developers/applications > New Application
    * then, once you've created the application, go to the "OAuth2" section in
    * the app's settings and generate a client secret, as well as specify
    * the Redirect URL ({server_url}/auth/discord/callback). */
    auth.OAuth.Discord("DISCORD_KEY", "DISCORD_SECRET")

    // . . . 

    // Mount the application
    app.Mount("/accounts", auth.App)

    // Serve assets that the account server depends on
    app.Static("/assets", "./assets")

    // . . . 

    // Run the app
    app.Listen("localhost:3000")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Accounts

type Accounts struct {
	APIv0 *v0.APIv0
	Page  *pages.Pages
	OAuth *oauth.OAuth
	App   *fiber.App
}

func New

func New(

	router_path string,

	server_url string,

	api_domain string,

	api_url string,

	server_name string,

	primary_website string,

	session_key string,

	enforce_https bool,

	db *sql.DB,

	db_flavor sqlbuilder.Flavor,

	email_config *types.MailConfig,

) *Accounts

New creates a new Accounts instance.

Accounts is a collection of API endpoints, pages, and OAuth endpoints that make up the Accounts service. It is meant to be mounted to a higher-level router.

The main purpose of New is to provide a simple way to create a new Accounts instance that is pre-configured with the most commonly used endpoints and settings.

type DatabaseConfig

type DatabaseConfig struct {
	Driver           string
	ConnectionString string
}

Directories

Path Synopsis
pkg
v0

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL