|
|
|
|
@ -1,11 +1,14 @@
@@ -1,11 +1,14 @@
|
|
|
|
|
package auth |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"encoding/json" |
|
|
|
|
"fmt" |
|
|
|
|
"github.com/gin-gonic/gin" |
|
|
|
|
"github.com/markbates/goth/gothic" |
|
|
|
|
"log" |
|
|
|
|
"net/http" |
|
|
|
|
db "sponsorahacker/db" |
|
|
|
|
"sponsorahacker/db" |
|
|
|
|
"strconv" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func Login(c *gin.Context) { |
|
|
|
|
@ -17,50 +20,108 @@ func Login(c *gin.Context) {
@@ -17,50 +20,108 @@ func Login(c *gin.Context) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func Callback(c *gin.Context) { |
|
|
|
|
dbclient, err := db.NewDbClient() |
|
|
|
|
// complete the authentication process
|
|
|
|
|
user, err := gothic.CompleteUserAuth(c.Writer, c.Request) |
|
|
|
|
|
|
|
|
|
// do the error checking
|
|
|
|
|
if err != nil { |
|
|
|
|
log.Println(err) |
|
|
|
|
log.Println("Error during user authentication:", err) |
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, "/login") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
sessionStore := NewSessionManager(dbclient) |
|
|
|
|
user, err := gothic.CompleteUserAuth(c.Writer, c.Request) |
|
|
|
|
// serialize the data
|
|
|
|
|
userData, err := json.Marshal(user) |
|
|
|
|
|
|
|
|
|
// check error for serialization
|
|
|
|
|
if err != nil { |
|
|
|
|
log.Println("Error during user authentication:", err) |
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, "/login") |
|
|
|
|
return |
|
|
|
|
log.Println("Error serializing user to json: ", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
dbClient, err := db.NewDbClient() |
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
log.Println("Error connecting to db: ", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = sessionStore.CreateSession(user, c) |
|
|
|
|
// new stuff
|
|
|
|
|
selectQuery := `SELECT id FROM users WHERE provider_userid = ? AND provider = ?` |
|
|
|
|
|
|
|
|
|
rows, err := dbClient.Query(selectQuery, user.UserID, user.Provider) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Println("failed to create session in db:", err) |
|
|
|
|
log.Fatalln("Error executing query:", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// For now, redirect to profile page after successful login
|
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, "/welcome") |
|
|
|
|
} |
|
|
|
|
defer rows.Close() |
|
|
|
|
|
|
|
|
|
func Logout(c *gin.Context) { |
|
|
|
|
dbClient, err := db.NewDbClient() |
|
|
|
|
var userId int64 |
|
|
|
|
|
|
|
|
|
if rows.Next() { |
|
|
|
|
err = rows.Scan(&userId) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Fatalln("Error scanning row:", err) |
|
|
|
|
} |
|
|
|
|
fmt.Println("User already exists with ID:", userId) |
|
|
|
|
} else { |
|
|
|
|
insertQuery := `INSERT INTO users (provider_userid, provider, nickname) VALUES (?, ?, ?)` |
|
|
|
|
result, err := dbClient.Exec(insertQuery, user.UserID, user.Provider, user.NickName) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Fatalln("Error inserting user:", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
lastInsertID, err := result.LastInsertId() |
|
|
|
|
if err != nil { |
|
|
|
|
log.Fatalln("Error getting last insert ID:", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
userId = lastInsertID |
|
|
|
|
} |
|
|
|
|
// old stuff
|
|
|
|
|
|
|
|
|
|
// unserialize so we can add more things
|
|
|
|
|
var userMap map[string]interface{} |
|
|
|
|
if err := json.Unmarshal(userData, &userMap); err != nil { |
|
|
|
|
log.Println("Error unmarshalling user JSON:", err) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
userMap["Sid"] = strconv.FormatInt(userId, 10) |
|
|
|
|
updatedUserData, err := json.Marshal(userMap) |
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
log.Println("Error serializing user JSON:", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// store data in a session
|
|
|
|
|
err = gothic.StoreInSession("user", string(updatedUserData), c.Request, c.Writer) |
|
|
|
|
|
|
|
|
|
// more error checking
|
|
|
|
|
if err != nil { |
|
|
|
|
log.Fatal(err) |
|
|
|
|
fmt.Println("Error saving user to session:", err) |
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save session"}) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
sessionStore := NewSessionManager(dbClient) |
|
|
|
|
// For now, redirect to welcome page
|
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, "/welcome") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func Logout(c *gin.Context) { |
|
|
|
|
session, err := gothic.Store.Get(c.Request, "_gothic-session") |
|
|
|
|
if err != nil { |
|
|
|
|
log.Fatal(err) |
|
|
|
|
fmt.Println("Error retrieving session:", err) |
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve session"}) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = sessionStore.DeleteSession(c) |
|
|
|
|
// Clear the session data
|
|
|
|
|
session.Values = make(map[interface{}]interface{}) |
|
|
|
|
|
|
|
|
|
// Save the empty session
|
|
|
|
|
err = session.Save(c.Request, c.Writer) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Fatal("failed to delete session:", err) |
|
|
|
|
fmt.Println("Error saving session: ", err) |
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save session"}) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, "/") |
|
|
|
|
|