basic

command
v0.0.0-...-159dada Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2025 License: MIT Imports: 8 Imported by: 0

README

Basic GoTrust Example 💡

This example demonstrates a basic implementation of GoTrust with in-memory storage.

Features

  • Email/password authentication
  • JWT token generation
  • Protected and public routes
  • Optional authentication middleware
  • OAuth integration ready (Google & GitHub)

Setup

  1. Set environment variables:
export JWT_SECRET="your-secret-key-at-least-32-characters-long"

# Optional: OAuth configuration
export GOOGLE_CLIENT_ID="your-google-client-id"
export GOOGLE_CLIENT_SECRET="your-google-client-secret"
export GITHUB_CLIENT_ID="your-github-client-id"
export GITHUB_CLIENT_SECRET="your-github-client-secret"
  1. Run the example:
go run main.go
  1. The server will start on http://localhost:8080

API Testing

1. Sign Up
curl -X POST http://localhost:8080/auth/signup \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "password": "password123",
    "name": "John Doe"
  }'
2. Sign In
curl -X POST http://localhost:8080/auth/signin \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "password": "password123"
  }'

Response:

{
  "user": {
    "id": "abc123",
    "email": "user@example.com",
    "name": "John Doe"
  },
  "access_token": "eyJhbGciOiJ...",
  "refresh_token": "eyJhbGciOiJ...",
  "expires_in": 86400
}
3. Access Protected Route
# Use the access_token from signin response
curl -X GET http://localhost:8080/api/profile \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN"
4. Refresh Token
curl -X POST http://localhost:8080/auth/refresh \
  -H "Content-Type: application/json" \
  -d '{
    "refresh_token": "YOUR_REFRESH_TOKEN"
  }'
5. Public Content (Optional Auth)
# Without authentication
curl -X GET http://localhost:8080/public/content

# With authentication (personalized)
curl -X GET http://localhost:8080/public/content \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN"
6. Logout
curl -X POST http://localhost:8080/auth/logout \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN"

OAuth Testing

Google OAuth
  1. Visit http://localhost:8080/auth/google in your browser
  2. Complete Google authentication
  3. You'll be redirected back with authentication tokens
GitHub OAuth
  1. Visit http://localhost:8080/auth/github in your browser
  2. Complete GitHub authentication
  3. You'll be redirected back with authentication tokens

Notes

  • This example uses in-memory storage which resets on server restart
  • For production, implement a proper database-backed UserStore
  • Configure real OAuth credentials for testing OAuth flows
  • The JWT secret should be stored securely in production

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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