forked from romshark/dgraph_graphql_go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
onAuth.go
46 lines (41 loc) · 904 Bytes
/
onAuth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package api
import (
"context"
"time"
"github.com/romshark/dgraph_graphql_go/store"
"github.com/romshark/dgraph_graphql_go/store/dgraph"
)
// onAuth is invoked by the transport layer during client authentication
func (srv *server) onAuth(
ctx context.Context,
sessionKey string,
) (userID store.ID, sessionCreationTime time.Time) {
// Search for the user session by key
var result struct {
Session []dgraph.Session `json:"session"`
}
if err := srv.store.QueryVars(
ctx,
`query Session($sessionKey: string) {
session(func: eq(Session.key, $sessionKey)) {
Session.creation
Session.user {
uid
User.id
}
}
}`,
map[string]string{
"$sessionKey": sessionKey,
},
&result,
); err != nil {
return
}
if len(result.Session) < 1 {
return
}
userID = store.ID(result.Session[0].User[0].ID)
sessionCreationTime = result.Session[0].Creation
return
}