flixoflax
06/14/2022, 7:17 AMnkshah2
06/14/2022, 7:21 AMsupertokens-auth-react
then the event firing should work out of the boxflixoflax
06/14/2022, 7:31 AM_app.tsx
I'm doing this: `
if (typeof window !== 'undefined') {
// we only want to call this init function on the frontend, so we check typeof window !== 'undefined'
SuperTokens.init({
appInfo: {
// learn more about this on https://supertokens.com/docs/thirdpartyemailpassword/appinfo
appName: process.env.NEXT_PUBLIC_SUPERTOKENS_APP_NAME as string,
apiDomain: process.env.NEXT_PUBLIC_SUPERTOKENS_API_DOMAIN as string,
websiteDomain: process.env.NEXT_PUBLIC_SUPERTOKENS_WEB_DOMAIN as string,
apiBasePath: process.env.NEXT_PUBLIC_SUPERTOKENS_API_BASE_PATH,
websiteBasePath: process.env.NEXT_PUBLIC_SUPERTOKENS_WEB_BASE_PATH,
},
recipeList: [
ThirdPartyEmailPassword.init({
signInAndUpFeature: {
providers: [Google.init(), Facebook.init()],
},
getRedirectionURL: async (context) => {
if (context.action === 'SUCCESS') {
if (context.redirectToPath !== undefined) {
// we are navigating back to where the user was before they authenticated
return context.redirectToPath
}
return '/dashboard'
}
return undefined
},
onHandleEvent: async (context) => {
if (context.action === 'SESSION_ALREADY_EXISTS') {
// TODO:
} else if (context.action === 'SUCCESS') {
const { id, email } = context.user
if (context.isNewUser) {
// TODO: Sign up
console.log('redirect to username setter')
} else {
// TODO: Sign in
console.log('User signed in :D')
}
console.log(id, email)
}
},
}),
Session.init(),
],
})
}
However there is no output in the console...nkshah2
06/14/2022, 7:33 AMflixoflax
06/14/2022, 7:34 AMflixoflax
06/14/2022, 7:35 AMpages/auth/[[...path]].tsx
import { useEffect } from 'react'
import SuperTokens from 'supertokens-auth-react'
import { redirectToAuth } from 'supertokens-auth-react/recipe/thirdpartyemailpassword'
import CustomAuthComponent from '../../components/CustomAuthComponent'
import ThirdPartyEmailPasswordAuthNoSSR from '../../components/ThirdPartyEmailPasswordAuthNoSSR'
export default function Auth() {
// if the user visits a page that is not handled by us (like /auth/random), then we redirect them back to the auth page.
useEffect(() => {
if (SuperTokens.canHandleRoute() === false) {
redirectToAuth()
}
}, [])
return (
<ThirdPartyEmailPasswordAuthNoSSR requireAuth={false}>
<CustomAuthComponent />
</ThirdPartyEmailPasswordAuthNoSSR>
)
}
flixoflax
06/14/2022, 7:36 AM<CustomAuthComponent />
```
```flixoflax
06/14/2022, 7:37 AMauthApi
import axios from 'axios'
import Session from 'supertokens-auth-react/recipe/session'
import { SigninForm, SignupForm } from '../schemas'
Session.addAxiosInterceptors(axios)
const API_URL =
(process.env.NEXT_PUBLIC_SUPERTOKENS_API_DOMAIN as string) +
(process.env.NEXT_PUBLIC_SUPERTOKENS_API_BASE_PATH as string)
const signUp = async (data: SignupForm) =>
axios.post(
`${API_URL}/signup`,
{
formFields: [
{
id: 'email',
value: data.email,
},
{
id: 'password',
value: data.password,
},
],
},
{
headers: {
rid: 'thirdpartyemailpassword',
'Content-Type': 'application/json',
},
}
)
const signIn = async (data: SigninForm) =>
axios.post(
`${API_URL}/signin`,
{
formFields: [
{
id: 'email',
value: data.email,
},
{
id: 'password',
value: data.password,
},
],
},
{
headers: {
rid: 'thirdpartyemailpassword',
'Content-Type': 'application/json',
},
}
)
const authApi = {
signUp,
signIn,
}
export default authApi
nkshah2
06/14/2022, 7:38 AMsupertokens-auth-react
are you using?rp_st
06/14/2022, 7:38 AMflixoflax
06/14/2022, 7:39 AMrp_st
06/14/2022, 7:39 AMflixoflax
06/14/2022, 7:39 AMrp_st
06/14/2022, 7:39 AMflixoflax
06/14/2022, 7:39 AMnkshah2
06/14/2022, 7:40 AMrp_st
06/14/2022, 7:40 AMThirdPartyEmailPassword.emailPasswordSignIn
etc (see https://supertokens.com/docs/auth-react/modules/recipe_thirdpartyemailpassword.html#emailPasswordSignIn-1)flixoflax
06/14/2022, 7:41 AMflixoflax
06/14/2022, 7:41 AMrp_st
06/14/2022, 7:43 AMnkshah2
06/14/2022, 7:44 AMflixoflax
06/14/2022, 8:14 AMflixoflax
06/14/2022, 8:16 AMrp_st
06/14/2022, 8:17 AM/
be default. If you want to change it, you can see https://supertokens.com/docs/thirdpartyemailpassword/common-customizations/redirecting-post-loginrp_st
06/14/2022, 8:18 AMflixoflax
06/14/2022, 8:20 AMrp_st
06/14/2022, 8:20 AMflixoflax
06/14/2022, 9:53 AMflixoflax
06/14/2022, 9:53 AMflixoflax
06/14/2022, 10:00 AMrp_st
06/14/2022, 10:06 AMflixoflax
06/14/2022, 11:03 AMflixoflax
06/14/2022, 11:03 AMflixoflax
06/14/2022, 11:10 AMrp_st
06/14/2022, 12:34 PMrp_st
06/14/2022, 12:35 PMflixoflax
06/14/2022, 1:53 PMgo get -u ./...
and a go mod tidy
seemed to fix the issuerp_st
06/14/2022, 2:01 PMflixoflax
06/14/2022, 2:05 PMflixoflax
06/14/2022, 2:06 PMrp_st
06/14/2022, 2:07 PMrp_st
06/14/2022, 2:07 PMflixoflax
06/14/2022, 2:07 PMflixoflax
06/14/2022, 2:07 PMflixoflax
06/14/2022, 2:07 PMrp_st
06/14/2022, 2:08 PMflixoflax
06/14/2022, 2:09 PMrp_st
06/14/2022, 2:10 PMflixoflax
06/14/2022, 2:12 PMrp_st
06/14/2022, 2:42 PMflixoflax
06/15/2022, 8:29 AMrp_st
06/15/2022, 8:30 AMflixoflax
06/15/2022, 8:44 AMrp_st
06/15/2022, 8:45 AMflixoflax
06/15/2022, 8:45 AMflixoflax
06/15/2022, 8:55 AMrp_st
06/15/2022, 9:11 AMflixoflax
06/15/2022, 11:41 AMflixoflax
06/15/2022, 4:06 PMrp_st
06/15/2022, 4:08 PMUpdateAccessTokenPayload
? Specifically, the response headersrp_st
06/15/2022, 4:12 PMrp_st
06/15/2022, 4:12 PMflixoflax
06/15/2022, 4:15 PMfunc SetUsername(c *fiber.Ctx) error {
sessionContainer := session.GetSessionFromRequestContext(c.UserContext())
if sessionContainer == nil {
return c.Status(500).JSON("No session found :(")
}
userId := sessionContainer.GetUserID()
db := database.DB
user := new(model.User)
err := c.BodyParser(user)
if err != nil {
return c.Status(500).JSON(fiber.Map{"status": "REVIEW_INPUT", "message": "Review your input", "data": err})
}
user.SuperTokensID = userId
err = db.Create(&user).Error
if err != nil {
return c.Status(500).JSON(fiber.Map{"status": "CANT_SET_USERNAME", "message": "Couldn't set a username for user with id " + userId})
}
currAccessTokenPayload := sessionContainer.GetAccessTokenPayload()
currAccessTokenPayload["username_set"] = true
err = sessionContainer.UpdateAccessTokenPayload(currAccessTokenPayload)
if err != nil {
c.Status(500).JSON(fiber.Map{"status": "UPDATE_ACCESSTOKEN_FAILED", "message": "Couldn't update the access token payload for user with id " + userId})
}
fmt.Println(sessionContainer.GetAccessTokenPayload())
return c.Status(200).JSON(fiber.Map{"status": "OK", "message": "Username successfuly set."})
}
Thats my handler... and this is my middleware : func VerifySessionWithoutUsername(options *sessmodels.VerifySessionOptions) fiber.Handler {
return func(c *fiber.Ctx) error {
adaptor.HTTPHandler(session.VerifySession(options, func(w http.ResponseWriter, r *http.Request) {
c.SetUserContext(r.Context())
}))(c)
if options != nil && !*options.SessionRequired {
return c.Next()
}
sessionContainer := session.GetSessionFromRequestContext(c.UserContext())
if sessionContainer != nil {
return c.Next()
} else {
return c.Status(401).JSON("Please authorize yourself.")
}
}
}
rp_st
06/15/2022, 4:18 PMrp_st
06/15/2022, 4:19 PMflixoflax
06/15/2022, 4:19 PMrp_st
06/15/2022, 4:32 PMgo
func verifySession(options *sessmodels.VerifySessionOptions) fiber.Handler {
return func(c *fiber.Ctx) error {
return adaptor.HTTPHandler(session.VerifySession(options, func(rw http.ResponseWriter, r *http.Request) {
//setting up the verified session context from http request to the fiber context
c.SetUserContext(r.Context())
c.Next()
}))(c)
}
}
rp_st
06/15/2022, 4:32 PMrp_st
06/15/2022, 4:34 PMc.Next()
in VerifySession
's callback.flixoflax
06/15/2022, 4:44 PMrp_st
06/15/2022, 4:45 PMflixoflax
06/15/2022, 4:46 PMrp_st
06/15/2022, 4:46 PMflixoflax
06/15/2022, 5:08 PMrp_st
06/15/2022, 6:45 PMgo
func verifySession(options *sessmodels.VerifySessionOptions) fiber.Handler {
return func(c *fiber.Ctx) error {
var httpResponse http.ResponseWriter
callbackCalled := false
adaptor.HTTPHandler(session.VerifySession(options, func(rw http.ResponseWriter, r *http.Request) {
callbackCalled = true
httpResponse = rw
//setting up the verified session context from http request to the fiber context
c.SetUserContext(r.Context())
}))(c)
if options != nil && !*options.SessionRequired {
err := c.Next()
if err != nil {
return err
}
}
if callbackCalled {
err := c.Next()
if err != nil {
return err
}
// the API may have modified the response headers, so we get that and set
// it in the fiber context
for key, valueArr := range httpResponse.Header() {
valueStr := ""
for i := 0; i < len(valueArr); i++ {
valueStr += valueArr[i]
if i < len(valueArr)-1 {
valueStr += ", "
}
}
c.Set(key, valueStr)
}
}
return nil
}
}
rp_st
06/15/2022, 6:45 PMflixoflax
06/15/2022, 6:45 PMrp_st
06/15/2022, 8:12 PMgo
func verifySession(options *sessmodels.VerifySessionOptions) fiber.Handler {
return func(c *fiber.Ctx) error {
return adaptor.HTTPHandlerFunc(http.HandlerFunc(session.VerifySession(options, func(rw http.ResponseWriter, r *http.Request) {
c.SetUserContext(r.Context())
err := c.Next()
if err != nil {
err = supertokens.ErrorHandler(err, r, rw)
if err != nil {
rw.WriteHeader(500)
_, _ = rw.Write([]byte(err.Error()))
}
return
}
c.Response().Header.VisitAll(func(k, v []byte) {
if string(k) == fasthttp.HeaderContentType {
rw.Header().Set(string(k), string(v))
}
})
rw.WriteHeader(c.Response().StatusCode())
_, _ = rw.Write(c.Response().Body())
})))(c)
}
}
flixoflax
06/15/2022, 8:29 PMflixoflax
06/16/2022, 6:54 AMflixoflax
06/16/2022, 6:54 AMrp_st
06/16/2022, 6:55 AMflixoflax
06/16/2022, 7:12 AMsreehari_jayaraj
08/27/2022, 6:45 PMsreehari_jayaraj
08/27/2022, 6:45 PMrp_st
08/28/2022, 5:39 AMrp_st
08/28/2022, 5:39 AM