Start Here
Quick Start

Quick Start

Get your application integrated with Optare in just a few minutes. This guide will walk you through the essential setup steps.

Installation

First, install the Optare SDK for your preferred language:

:::code-group

npm install @optare/sdk
# Using yarn
yarn add @optare/sdk
# Using pnpm
pnpm add @optare/sdk

:::

Backend Setup

Configure the backend authentication in your application:

:::code-group

import { betterAuth } from "better-auth";
 
export const auth = betterAuth({
  secret: process.env.BETTER_AUTH_SECRET!,
  baseURL: process.env.NEXT_PUBLIC_APP_URL || "http://localhost:3000",
  
  socialProviders: {
    optare: {
      clientId: process.env.OPTARE_CLIENT_ID!,
      clientSecret: process.env.OPTARE_CLIENT_SECRET!,
      authorizationURL: "https://id.optare.one/oauth/authorize",
      tokenURL: "https://id.optare.one/oauth/token",
      userInfoURL: "https://id.optare.one/oauth/userinfo",
    },
  },
});
from optare import OptareAuth
 
auth = OptareAuth(
    client_id=os.getenv("OPTARE_CLIENT_ID"),
    client_secret=os.getenv("OPTARE_CLIENT_SECRET"),
    redirect_uri="http://localhost:5000/callback",
    authorization_url="https://id.optare.one/oauth/authorize",
    token_url="https://id.optare.one/oauth/token",
    userinfo_url="https://id.optare.one/oauth/userinfo"
)
package main
 
import (
    "github.com/optare/optare-go"
    "os"
)
 
func main() {
    auth := optare.NewAuth(&optare.Config{
        ClientID:     os.Getenv("OPTARE_CLIENT_ID"),
        ClientSecret: os.Getenv("OPTARE_CLIENT_SECRET"),
        RedirectURI:  "http://localhost:8080/callback",
        AuthURL:      "https://id.optare.one/oauth/authorize",
        TokenURL:     "https://id.optare.one/oauth/token",
        UserInfoURL:  "https://id.optare.one/oauth/userinfo",
    })
}

:::

Frontend Integration

Add the Optare login button to your application:

:::code-group

import { authClient } from "~/lib/auth-client";
 
export function LoginButton() {
  const handleLogin = async () => {
    await authClient.signIn.social({
      provider: "optare",
      callbackURL: "/dashboard",
    });
  };
 
  return (
    <button onClick={handleLogin}>
      Sign in with Optare
    </button>
  );
}
from flask import redirect, url_for
 
@app.route('/login')
def login():
    return redirect(auth.get_authorization_url())
 
@app.route('/callback')
def callback():
    token = auth.exchange_code(request.args.get('code'))
    user = auth.get_user_info(token)
    session['user'] = user
    return redirect('/dashboard')
func LoginHandler(w http.ResponseWriter, r *http.Request) {
    authURL := auth.GetAuthorizationURL()
    http.Redirect(w, r, authURL, http.StatusTemporaryRedirect)
}
 
func CallbackHandler(w http.ResponseWriter, r *http.Request) {
    code := r.URL.Query().Get("code")
    token, err := auth.ExchangeCode(code)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    
    user, err := auth.GetUserInfo(token)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    
    // Store user session
    http.Redirect(w, r, "/dashboard", http.StatusTemporaryRedirect)
}

:::

Protecting Routes

Secure your API routes and pages:

:::code-group

import { auth } from "~/auth.server";
 
export async function loader({ request }: LoaderFunctionArgs) {
  const session = await auth.api.getSession({
    headers: request.headers,
  });
 
  if (!session) {
    throw redirect("/login");
  }
 
  return json({ user: session.user });
}
from functools import wraps
from flask import session, redirect
 
def require_auth(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if 'user' not in session:
            return redirect('/login')
        return f(*args, **kwargs)
    return decorated_function
 
@app.route('/dashboard')
@require_auth
def dashboard():
    user = session['user']
    return render_template('dashboard.html', user=user)
func RequireAuth(next http.HandlerFunc) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        session, _ := store.Get(r, "session")
        
        if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
            http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
            return
        }
        
        next(w, r)
    }
}
 
func DashboardHandler(w http.ResponseWriter, r *http.Request) {
    session, _ := store.Get(r, "session")
    user := session.Values["user"]
    // Render dashboard
}
 
// Usage
http.HandleFunc("/dashboard", RequireAuth(DashboardHandler))

:::

Making API Calls

:::code-group

POST https://id.optare.one/oauth/token
Content-Type: application/x-www-form-urlencoded
 
grant_type=authorization_code&
code=AUTH_CODE&
client_id=YOUR_CLIENT_ID&
client_secret=YOUR_CLIENT_SECRET&
redirect_uri=YOUR_REDIRECT_URI
curl -X POST https://id.optare.one/oauth/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=authorization_code" \
  -d "code=AUTH_CODE" \
  -d "client_id=YOUR_CLIENT_ID" \
  -d "client_secret=YOUR_CLIENT_SECRET" \
  -d "redirect_uri=YOUR_REDIRECT_URI"

:::

Next Steps

Now that you have the basics set up, explore these advanced features: