Golang
Latest library version: 3.2.2

Front end:

Include the library SDK:
1
var el = document.createElement("script");
2
el.async = true;
3
el.defer = true;
4
el.src = "https://cdn.jsdelivr.net/npm/[email protected]${version}/dist/oneaccount.min.js";
5
document.body.appendChild(el);
Copied!
Or just include the script in your HTML (at the end of your body tag):
1
<script defer async src="https://cdn.jsdelivr.net/npm/[email protected]<version>/dist/oneaccount.min.js"></script>
Copied!
Initialize the library:
1
if (window.oneaccount) {
2
initOneaccount();
3
} else {
4
document.addEventListener("oneaccount-loaded", initOneaccount);
5
}
6
7
// init the library
8
function initOneaccount() {
9
window.oneaccount.init("your external id", {
10
// NOTE: Please check the Library options page for more customisations
11
iOSRedirectURL: "/user", // required
12
callbackURL: "/oneaccountauth", // required
13
});
14
}
Copied!
Add the button:
1
<!-- NOTE: the classes are important for the library to attach the click bindings -->
2
<button class="oneaccount-button oneaccount-show">Sign in/up using One account</button>
Copied!
1
document.addEventListener("oneaccount-authenticated", function(event) {
2
// data contains any data you have returned from the backend
3
var data = event.detail;
4
// your business logic here
5
});
Copied!

Backend:

Setup the library:
1
package main
2
3
import (
4
"encoding/json"
5
"net/http"
6
"os"
7
8
"github.com/go-redis/redis/v8"
9
"github.com/oilastudio/oneaccount-go"
10
)
11
12
func main() {
13
var redisClient = redis.NewClient(&redis.Options{})
14
15
oa := oneaccount.New(
16
// If engine setter and getter are not set an in-memory engine will be used.
17
// For production it is recommended to provide an engine setter and getter:
18
// for this example we will use redis but any other storage could be used
19
oneaccount.SetEngineSetter(func(ctx context.Context, k string, v []byte) error {
20
return redisClient.Set(ctx, k, v, 3 * time.Minute).Err()
21
}),
22
oneaccount.SetEngineGetter(func(ctx context.Context, k string) ([]byte, error) {
23
v, err := redisClient.Get(ctx, k).Result()
24
if err != nil {
25
return nil, err
26
}
27
return []byte(v), redisClient.Del(ctx, k).Err()
28
}),
29
)
30
31
// The route URL is the callback URL you have set when you created One account app.
32
http.Handle("/oneaccountauth", oa.Auth(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
33
// NOTE: never return code 200 if a user is not authenticated
34
if !oneaccount.IsAuthenticated(r) {
35
http.Error(w, "something went wrong while authenticating, please try again later", http.StatusInternalServerError)
36
return
37
}
38
// a user is authenticated and you can implement any logic your application
39
// needs. oneaccount.Data(r) returns the data sent by the user
40
// after successful authentication.
41
42
// since One account doesn't differentiate between sign up and sign in,
43
// you can use userId to check if the user signed up c on your website or not.
44
// the same way you can access any other data you requested from the user:
45
type User struct {
46
FirstName string `json:"firstName"`
47
UserID int `json:"userId"`
48
}
49
50
var data = user{}
51
// the object contains all fields (camelCased) that are defined in the app (Requested data)
52
if err := json.Unmarshal(oneaccount.Data(r), &data); err != nil {
53
http.Error(w, err.Error(), http.StatusBadRequest)
54
return
55
}
56
// any data returned here would be sent to oneaccount-authenticated event on front-end e.g.:
57
w.Header().Set("Content-Type", "application/json; charset=utf-8")
58
json.NewEncoder(w).Encode(data)
59
// NOTE: handle the error
60
})))
61
}
Copied!
For more options of authentication please check the library documentation: https://github.com/oilastudio/oneaccount-go
Last modified 2mo ago
Copy link