Clerk logo

Clerk Docs

Ctrl + K
Go to clerk.devGet API keys

Verifying a session

Protect Your Backend APIs

Go makes it really easy to create a simple HTTP server, and Clerk makes it really easy to authenticate any request. In the following example you can learn how to verify a session and retrieve the corresponding user.

1
package main
2
3
import (
4
"net/http"
5
"strings"
6
7
"github.com/clerkinc/clerk-sdk-go/clerk"
8
)
9
10
func main() {
11
client, _ := clerk.NewClient("CLERK_API_KEY")
12
13
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
14
// get session token from Authorization header
15
sessionToken := r.Header.Get("Authorization")
16
sessionToken = strings.TrimPrefix(sessionToken, "Bearer ")
17
18
// verify the session
19
sessClaims, err := client.VerifyToken(sessionToken)
20
if err != nil {
21
w.WriteHeader(http.StatusUnauthorized)
22
w.Write([]byte("Unauthorized"))
23
return
24
}
25
26
// get the user, and say welcome!
27
user, err := client.Users().Read(sessClaims.Subject)
28
if err != nil {
29
panic(err)
30
}
31
32
w.Write([]byte("Welcome " + *user.FirstName))
33
})
34
35
http.ListenAndServe(":8080", nil)
36
}
37
1
package main
2
3
import (
4
"net/http"
5
6
"github.com/clerkinc/clerk-sdk-go/clerk"
7
)
8
9
func main() {
10
client, _ := clerk.NewClient("CLERK_API_KEY")
11
12
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
13
// verify the session
14
sess, err := client.Verification().Verify(r)
15
if err != nil {
16
w.WriteHeader(http.StatusUnauthorized)
17
w.Write([]byte("Unauthorized"))
18
return
19
}
20
21
// Optionally get the user, and say welcome!
22
user, err := client.Users().Read(sess.UserID)
23
if err != nil {
24
panic(err)
25
}
26
27
w.Write([]byte("Welcome " + *user.FirstName))
28
})
29
30
http.ListenAndServe(":8080", nil)
31
}
32

Using middleware

The Clerk SDK also provides a simple middleware that adds the active session to the request's context.

1
package main
2
3
import (
4
"net/http"
5
6
"github.com/clerkinc/clerk-sdk-go/clerk"
7
)
8
9
func main() {
10
client, _ := clerk.NewClient("CLERK_API_KEY")
11
12
mux := http.NewServeMux()
13
14
injectActiveSession := clerk.WithSession(client)
15
mux.Handle("/hello", injectActiveSession(helloUserHandler(client)))
16
17
http.ListenAndServe(":8080", mux)
18
}
19
20
func helloUserHandler(client clerk.Client) http.HandlerFunc {
21
return func(w http.ResponseWriter, r *http.Request) {
22
ctx := r.Context()
23
24
sessClaims, ok := ctx.Value(clerk.ActiveSessionClaims).(*clerk.SessionClaims)
25
if !ok {
26
w.WriteHeader(http.StatusUnauthorized)
27
w.Write([]byte("Unauthorized"))
28
return
29
}
30
31
user, err := client.Users().Read(sessClaims.Subject)
32
if err != nil {
33
panic(err)
34
}
35
36
w.Write([]byte("Welcome " + *user.FirstName))
37
}
38
}
39
1
package main
2
3
import (
4
"net/http"
5
6
"github.com/clerkinc/clerk-sdk-go/clerk"
7
)
8
9
func main() {
10
client, _ := clerk.NewClient("CLERK_API_KEY")
11
12
mux := http.NewServeMux()
13
14
injectActiveSession := clerk.WithSession(client)
15
mux.Handle("/hello", injectActiveSession(helloUserHandler(client)))
16
17
http.ListenAndServe(":8080", mux)
18
}
19
20
func helloUserHandler(client clerk.Client) http.HandlerFunc {
21
return func(w http.ResponseWriter, r *http.Request) {
22
ctx := r.Context()
23
24
sess, ok := ctx.Value(clerk.ActiveSession).(*clerk.Session)
25
if !ok {
26
w.WriteHeader(http.StatusUnauthorized)
27
w.Write([]byte("Unauthorized"))
28
return
29
}
30
31
// Optionally get the user, and say welcome!
32
user, err := client.Users().Read(sess.UserID)
33
if err != nil {
34
panic(err)
35
}
36
37
w.Write([]byte("Welcome " + *user.FirstName))
38
}
39
}
40

Was this helpful?

Clerk © 2022