Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Use os.Getenv #8

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions 03-Gin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,31 @@ package main

import (
"net/http"
"os"

"github.com/gin-gonic/gin"
"github.com/passageidentity/passage-go"
)

func authRequired() gin.HandlerFunc {
return func(c *gin.Context) {
psg, _ := passage.New("<Passage App ID>", &passage.Config{
APIKey: "<Passage API Key>",
psg, _ := passage.New(os.Getenv("PASSAGE_APP_ID"), &passage.Config{
APIKey: os.Getenv("PASSAGE_API_KEY"),
})
passageUserID, err := psg.AuthenticateRequest(c.Request)
if err != nil {
// Authentication failed!
c.HTML(http.StatusForbidden, "unauthorized.html", nil)
c.Abort()
return
}

// Get the authenticated user's email and set it in the context
passageUser, err := psg.GetUser(passageUserID)
if err != nil {
// This should not fail, but abort the request if it does
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.Set("userEmail", passageUser.Email)

Expand Down