Skip to content
Snippets Groups Projects
Commit 7cb693bc authored by Fredrik Grøttås's avatar Fredrik Grøttås :speech_balloon:
Browse files

Merge branch 'method-checking' into 'main'

Method checking

See merge request !43
parents c0e14082 1bedadf3
No related branches found
No related tags found
1 merge request!43Method checking
......@@ -3,10 +3,15 @@ package handlers
import (
firebase "go-firebase-auth/Firebase"
"net/http"
"strings"
"github.com/solovev/steam_go"
)
var host = "10.212.171.189:8080"
var baseURL = "http://" + host
var userpage = baseURL + "/userpage"
var SteamValidateAndGetId func(*steam_go.OpenId, *http.Request) (string, error) = func(opId *steam_go.OpenId, r *http.Request) (string, error) {
return opId.ValidateAndGetId()
}
......@@ -14,9 +19,11 @@ var SteamValidateAndGetId func(*steam_go.OpenId, *http.Request) (string, error)
// Connect a user to their Steam account. Will take the user to Steam authorization
func SteamConnect(w http.ResponseWriter, r *http.Request) {
opId := steam_go.NewOpenId(r)
switch opId.Mode() {
case "":
http.Redirect(w, r, opId.AuthUrl(), http.StatusMovedPermanently)
authURL := strings.Replace(opId.AuthUrl(), "servers", host, 1)
http.Redirect(w, r, authURL, http.StatusMovedPermanently)
case "cancel":
w.Write([]byte("Cancelled Steam authorization"))
......@@ -25,7 +32,7 @@ func SteamConnect(w http.ResponseWriter, r *http.Request) {
// Get the Steam id
steamId, err := SteamValidateAndGetId(opId, r)
if err != nil {
http.Redirect(w, r, "/userpage", http.StatusInternalServerError)
http.Redirect(w, r, userpage, http.StatusInternalServerError)
return
}
......@@ -33,7 +40,7 @@ func SteamConnect(w http.ResponseWriter, r *http.Request) {
token := r.URL.Query().Get("token")
userId, err := firebase.GetUserId(token)
if err != nil {
http.Redirect(w, r, "/userpage", http.StatusBadRequest)
http.Redirect(w, r, userpage, http.StatusBadRequest)
return
}
......@@ -41,14 +48,14 @@ func SteamConnect(w http.ResponseWriter, r *http.Request) {
path := "users/" + userId
doc, err := firebase.DB.GetDoc(path)
if err != nil {
http.Redirect(w, r, "/userpage", http.StatusInternalServerError)
http.Redirect(w, r, userpage, http.StatusInternalServerError)
return
}
prevSteamId, ok := doc.Data["steamId"].(string)
if ok {
if steamId == prevSteamId {
http.Redirect(w, r, "/userpage", http.StatusConflict)
http.Redirect(w, r, userpage, http.StatusConflict)
return
}
}
......@@ -58,11 +65,11 @@ func SteamConnect(w http.ResponseWriter, r *http.Request) {
"steamId": steamId,
})
if err != nil {
http.Redirect(w, r, "/userpage", http.StatusInternalServerError)
http.Redirect(w, r, userpage, http.StatusInternalServerError)
return
}
// Return to user page
http.Redirect(w, r, "/userpage", http.StatusMovedPermanently)
http.Redirect(w, r, userpage, http.StatusMovedPermanently)
}
}
module go-firebase-auth
go 1.21.11
go 1.22.0
toolchain go1.23.0
......
......@@ -40,40 +40,40 @@ func main() {
http.ServeFile(w, r, filepath.Join("./static", "games.html"))
})
http.HandleFunc("/getgenres", handlers.GetGenres)
http.HandleFunc("/gamepage", handlers.GamePage)
http.HandleFunc("/getusergamedata", handlers.GetUserGameData)
http.HandleFunc("/searchgames", handlers.SearchGames)
http.HandleFunc("/registeruser", handlers.RegisterUser)
http.HandleFunc("/submitusergamedata", handlers.SubmitUserGameData)
http.HandleFunc("/deletereview", handlers.DeleteReview)
http.HandleFunc("/addtojournal", handlers.AddToJournal)
http.HandleFunc("/removefromjournal", handlers.RemoveFromJournal)
http.HandleFunc("/steamconnect", handlers.SteamConnect)
http.HandleFunc("/steamdisconnect", handlers.SteamDisconnect)
http.HandleFunc("/connectedtosteam", handlers.ConnectedToSteam)
http.HandleFunc("/getusernameandprivacy", handlers.GetUsernameAndPrivacy)
http.HandleFunc("/updateusername", handlers.UpdateUsername)
http.HandleFunc("/getuserjournal", handlers.GetUserJournal)
http.HandleFunc("/getreviewsforgame", handlers.GetReviewsForGame)
http.HandleFunc("/sendmessage", handlers.SendMessage)
http.HandleFunc("/getmessages", handlers.GetMessages)
http.HandleFunc("/getuser", handlers.GetUser)
http.HandleFunc("/deleteUser", handlers.DeleteUser)
http.HandleFunc("/submitusergamecomment", handlers.SubmitUserGameComment)
http.HandleFunc("/getusergamecomments", handlers.GetUserGameComments)
http.HandleFunc("/toggleprivacy", handlers.UpdatePrivacy)
http.HandleFunc("/otheruserpage", handlers.OtherUserpage)
http.HandleFunc("/follow", handlers.Follow)
http.HandleFunc("/unfollow", handlers.Unfollow)
http.HandleFunc("/getfeed", handlers.GetFeed)
http.HandleFunc("/getgame", handlers.GetGame)
http.HandleFunc("/getgamerecommendations", handlers.GetGameRecs)
http.HandleFunc("/gennewrecommendations", handlers.GenNewRecs)
http.HandleFunc("/getfollowers", handlers.GetFollowers)
http.HandleFunc("/getfollowing", handlers.GetFollowing)
http.HandleFunc("/getchats", handlers.GetChats)
http.HandleFunc("/deletecomment", handlers.DeleteComment)
http.HandleFunc("GET /getgenres/", handlers.GetGenres)
http.HandleFunc("GET /gamepage/", handlers.GamePage)
http.HandleFunc("GET /getusergamedata", handlers.GetUserGameData)
http.HandleFunc("GET /searchgames", handlers.SearchGames)
http.HandleFunc("POST /registeruser", handlers.RegisterUser)
http.HandleFunc("POST /submitusergamedata", handlers.SubmitUserGameData)
http.HandleFunc("DELETE /deletereview", handlers.DeleteReview)
http.HandleFunc("POST /addtojournal", handlers.AddToJournal)
http.HandleFunc("DELETE /removefromjournal", handlers.RemoveFromJournal)
http.HandleFunc("GET /steamconnect", handlers.SteamConnect)
http.HandleFunc("DELETE /steamdisconnect", handlers.SteamDisconnect)
http.HandleFunc("GET /connectedtosteam", handlers.ConnectedToSteam)
http.HandleFunc("GET /getusernameandprivacy", handlers.GetUsernameAndPrivacy)
http.HandleFunc("POST /updateusername", handlers.UpdateUsername)
http.HandleFunc("GET /getuserjournal", handlers.GetUserJournal)
http.HandleFunc("GET /getreviewsforgame", handlers.GetReviewsForGame)
http.HandleFunc("POST /sendmessage", handlers.SendMessage)
http.HandleFunc("GET /getmessages", handlers.GetMessages)
http.HandleFunc("GET /getuser", handlers.GetUser)
http.HandleFunc("DELETE /deleteUser", handlers.DeleteUser)
http.HandleFunc("POST /submitusergamecomment", handlers.SubmitUserGameComment)
http.HandleFunc("GET /getusergamecomments", handlers.GetUserGameComments)
http.HandleFunc("POST /toggleprivacy", handlers.UpdatePrivacy)
http.HandleFunc("GET /otheruserpage", handlers.OtherUserpage)
http.HandleFunc("POST /follow", handlers.Follow)
http.HandleFunc("DELETE /unfollow", handlers.Unfollow)
http.HandleFunc("GET /getfeed", handlers.GetFeed)
http.HandleFunc("GET /getgame", handlers.GetGame)
http.HandleFunc("GET /getgamerecommendations", handlers.GetGameRecs)
http.HandleFunc("GET /gennewrecommendations", handlers.GenNewRecs)
http.HandleFunc("GET /getfollowers", handlers.GetFollowers)
http.HandleFunc("GET /getfollowing", handlers.GetFollowing)
http.HandleFunc("GET /getchats", handlers.GetChats)
http.HandleFunc("DELETE /deletecomment", handlers.DeleteComment)
fmt.Printf("Starting server at port " + port + "...\n")
err = http.ListenAndServe(":"+port, nil)
......
......@@ -30,7 +30,7 @@
<p id="name"></p>
<div id = "chat"></div>
<form id="messageForm">
<input id="content" type="text">
<input id="content" type="text", maxlength="150">
<button id="sendMessage" type="button">Send</button>
</form>
......
......@@ -115,7 +115,7 @@ async function loadMessages(page, pageSize, offset, first) {
function displayMessages(messages) {
eChat.innerHTML = "";
//messages = messages.reverse();
messages.forEach(m => {
// Create a message element
let message = document.createElement("div");
......@@ -131,7 +131,7 @@ function displayMessages(messages) {
async function sendMessage() {
const content = escapeHTML(eMessageContent.value);
if(content != "") {
if(content != "" && content.length <= 150) {
let idToken = await currentUser.getIdToken();
const request = "/sendmessage"
......
......@@ -267,7 +267,7 @@ async function removeFromJournal() {
"gameId": gameId
});
let response = await sendRequest(request, "POST", body, userRequests());
let response = await sendRequest(request, "DELETE", body, userRequests());
if(response.ok) {
alert("Successfully removed from journal.");
location.reload();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment