forked from supabase/auth
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(refactor): remove dependency on gothic sessions
- Loading branch information
1 parent
46df5fd
commit 1922234
Showing
5 changed files
with
48 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package storage | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/gorilla/securecookie" | ||
"github.com/gorilla/sessions" | ||
"github.com/kelseyhightower/envconfig" | ||
) | ||
|
||
var sessionName = "_gotrue_session" | ||
var Store sessions.Store | ||
|
||
type SessionConfig struct { | ||
Key []byte `envconfig:"GOTRUE_SESSION_KEY"` | ||
} | ||
|
||
func init() { | ||
var sessionConfig SessionConfig | ||
err := envconfig.Process("GOTRUE_SESSION_KEY", &sessionConfig) | ||
if err != nil || len(sessionConfig.Key) == 0 { | ||
sessionConfig.Key = securecookie.GenerateRandomKey(32) | ||
} | ||
Store = sessions.NewCookieStore(sessionConfig.Key) | ||
} | ||
|
||
func StoreInSession(key string, value string, req *http.Request, res http.ResponseWriter) error { | ||
session, _ := Store.New(req, sessionName) | ||
session.Values[key] = value | ||
return session.Save(req, res) | ||
} | ||
|
||
func GetFromSession(key string, req *http.Request) (string, error) { | ||
session, _ := Store.Get(req, sessionName) | ||
value, ok := session.Values[key] | ||
if !ok { | ||
return "", errors.New("session could not be found for this request") | ||
} | ||
|
||
return value.(string), nil | ||
} |