-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: connected user email into DB and auth
feat: connected user email into DB and auth
- Loading branch information
Showing
7 changed files
with
105 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { getAuth } from 'https://www.gstatic.com/firebasejs/10.14.1/firebase-auth.js' | ||
|
||
function processForm(e) { | ||
//e.preventDefault(); | ||
|
||
// Emit 'msg' event for bot progress bar | ||
const event = new Event("msg"); | ||
document.dispatchEvent(event); | ||
|
||
// Get the user email address | ||
const auth = getAuth(); | ||
const user = auth.currentUser; | ||
|
||
// If the user hasn't signed in, redirect them to the sign-in page. | ||
if (!user) { | ||
window.location = `/?status=unauthorized`; | ||
} | ||
return true; | ||
} | ||
|
||
window.addEventListener("load", function() { | ||
const form = document.getElementById("userMsg"); | ||
if (form.attachEvent) { | ||
form.attachEvent("submit", processForm); | ||
} else { | ||
form.addEventListener("submit", processForm); | ||
} | ||
}); |
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,14 @@ | ||
import { getAuth, onAuthStateChanged } from 'https://www.gstatic.com/firebasejs/10.14.1/firebase-auth.js' | ||
|
||
window.addEventListener("load", function () { | ||
const auth = getAuth(); | ||
const user = auth.currentUser; | ||
|
||
onAuthStateChanged(auth, (user) => { | ||
if (user) { | ||
const uid = user.uid; | ||
} else { | ||
window.location = `/?message=sign-in-required`; | ||
} | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -11,8 +11,10 @@ import ( | |
) | ||
|
||
var ( | ||
projectID string | ||
userEmail string = "[email protected]" | ||
r *gin.Engine | ||
projectID string | ||
userEmail string = "[email protected]" | ||
userEmailParam string = "user" | ||
) | ||
|
||
type ClientError struct { | ||
|
@@ -30,7 +32,7 @@ func main() { | |
} | ||
LogInfo("Starting Herodotus...") | ||
|
||
r := gin.Default() | ||
r = gin.Default() | ||
r.LoadHTMLGlob("templates/*") | ||
r.Static("/js", "./js") | ||
r.StaticFile("/favicon.ico", "./favicon.ico") | ||
|
@@ -49,22 +51,31 @@ func login(c *gin.Context) { | |
} | ||
|
||
func startConversation(c *gin.Context) { | ||
params := c.Params | ||
log.Println(params) | ||
|
||
// extractParams will redirect if user isn't logged in. | ||
userEmail = extractParams(c) | ||
|
||
LogInfo("Start conversation request received") | ||
|
||
c.HTML(http.StatusOK, "index.html", gin.H{ | ||
"Message": struct { | ||
Message string | ||
Email string | ||
}{ | ||
Message: "Hello! I hear that you want to go on a trip somewhere. Tell me about it.", | ||
Email: userEmail, | ||
}, | ||
}) | ||
} | ||
|
||
func respondToUser(c *gin.Context) { | ||
|
||
// extractParams will redirect if user isn't logged in. | ||
userEmail = extractParams(c) | ||
|
||
LogInfo("Respond to user request received") | ||
|
||
// Parse Form | ||
c.Request.ParseForm() | ||
userMsg := c.Request.Form["userMsg"][0] | ||
log.Println(userMsg) | ||
|
@@ -92,8 +103,10 @@ func respondToUser(c *gin.Context) { | |
c.HTML(http.StatusOK, "index.html", gin.H{ | ||
"Message": struct { | ||
Message string | ||
Email string | ||
}{ | ||
Message: botResponse, | ||
Email: userEmail, | ||
}, | ||
}) | ||
} | ||
|
@@ -110,3 +123,14 @@ func clientError(c *gin.Context) { | |
LogError(fmt.Sprintf("clientError: %s, %s, %s\n", cError.Code, cError.Message, cError.Email)) | ||
c.JSON(http.StatusOK, gin.H{"error": "message logged"}) | ||
} | ||
|
||
func extractParams(c *gin.Context) string { | ||
// Verify that the user is signed in before answering | ||
userEmail = c.Query(userEmailParam) | ||
if userEmail == "" { | ||
LogWarning("User attempted to navigate directly to /home; redirected to sign-in") | ||
c.Request.URL.Path = "/" | ||
r.HandleContext(c) | ||
} | ||
return userEmail | ||
} |
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