Get Started

Installation

Install AuthInGo, create the PostgreSQL schema, mount Go auth routes, and connect your first React app.

Installation

This guide creates a Go backend mounted at http://localhost:8080/api/auth and a React or Next.js frontend that talks to it.

Install the Go packages

Create or open a Go module, then install the core package and the PostgreSQL adapter.

go get github.com/binit2-1/authingo
go get github.com/binit2-1/authingo/adapters/postgres
go get github.com/jackc/pgx/v5/stdlib

Prepare the database tables

The PostgreSQL adapter ships an idempotent schema helper. For local development and small demos, call postgres.ApplySchema(ctx, db) after opening your database connection in the server code below.

For production applications, put the same schema into your normal migration tool. The full SQL lives in adapters/postgres/schema.sql.

Mount the auth handler

Create the AuthInGo engine with a storage adapter, then mount the built-in handler under /api/auth.

main.go
package main

import (
	"context"
	"database/sql"
	"log"
	"net/http"
	"os"
	"time"

	"github.com/binit2-1/authingo"
	"github.com/binit2-1/authingo/adapters/postgres"
	_ "github.com/jackc/pgx/v5/stdlib"
)

func main() {
	db, err := sql.Open("pgx", os.Getenv("DATABASE_URL"))
	if err != nil {
		log.Fatal(err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	if err := postgres.ApplySchema(ctx, db); err != nil {
		log.Fatal(err)
	}

	auth := authingo.New(authingo.Options{
		Store: postgres.NewAdapter(db),
	})

	mux := http.NewServeMux()
	mux.Handle("/api/auth/", http.StripPrefix("/api/auth", auth.Handler()))

	log.Println("listening on http://localhost:8080")
	log.Fatal(http.ListenAndServe(":8080", mux))
}

Install the React SDK

Install the frontend package in your React or Next.js app.

npm install @authingo/react
pnpm add @authingo/react
yarn add @authingo/react

Wrap your app

Add AuthProvider near the root of your app and point baseURL at your mounted Go auth routes.

app/providers.tsx
"use client";

import { AuthProvider } from "@authingo/react";

export function Providers({ children }: { children: React.ReactNode }) {
  return (
    <AuthProvider baseURL="http://localhost:8080/api/auth">
      {children}
    </AuthProvider>
  );
}
app/layout.tsx
import { Providers } from "./providers";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        <Providers>{children}</Providers>
      </body>
    </html>
  );
}

Cross-origin frontends

If your frontend runs on another origin, configure CORS to allow credentials and the X-Authingo-Client header. The React SDK sends cookies with credentials: "include".

Next steps

On this page