Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Access specific values from claims interface{} #3

Closed
Anthonyhawkins opened this issue Sep 16, 2021 · 3 comments
Closed

How to Access specific values from claims interface{} #3

Anthonyhawkins opened this issue Sep 16, 2021 · 3 comments

Comments

@Anthonyhawkins
Copy link

This is more likely my lack of understanding of how Go works than a gofiber-firebaseauth specific problem.

In the examples it shows accessing the claims object from context and printing it out.

claims := c.Locals("user")
fmt.Println(claims)

This works and I see this

{false mzeijVVkt6MDFQWmWBgzqr79taq2 ah@gmail.com}

From looking at the source, it loads in the private user struct into the context which is populated by fields from Token from firebase. This results in a generic interface{} for claims and I cannot figure out how to access each field, individually.

I looked into type assertion, reflection etc. but not sure what is needed here.

Thanks for the help.

@yasumuo
Copy link

yasumuo commented Sep 23, 2021

This is more likely my lack of understanding of how Go works than a gofiber-firebaseauth specific problem.

In the examples it shows accessing the claims object from context and printing it out.

claims := c.Locals("user")
fmt.Println(claims)

This works and I see this

{false mzeijVVkt6MDFQWmWBgzqr79taq2 ah@gmail.com}

From looking at the source, it loads in the private user struct into the context which is populated by fields from Token from firebase. This results in a generic interface{} for claims and I cannot figure out how to access each field, individually.

I looked into type assertion, reflection etc. but not sure what is needed here.

Thanks for the help.

I was struggling with a similar problem (I'm also new to Go and still learning).

In my case, I wanted to use the value of the email field, and after a lot of research, I was able to get it by using reflection.
The following is the code that solved my case. (I'm running recover to deal with the panic)

defer func() error {
			err := recover()
			if err != nil {
				log.Println("Recover!: ", err)
			}
			return nil
		}()

fu := c.Locals("user")
rfuv := reflect.ValueOf(fu)
rfuv2 := reflect.New(rfuv.Type()).Elem()
rfuv2.Set(rfuv)
rfuv = rfuv2.FieldByName("email")
rfuv = reflect.NewAt(rfuv.Type(), unsafe.Pointer(rfuv.UnsafeAddr())).Elem()
fuvstrptr := (*string)(unsafe.Pointer(rfuv.UnsafeAddr()))
fuvstr := *fuvstrptr

log.Println("User Email: ", fuvstr)

I don't think it's a good solution, and I too would like to have something to access each field 😅

@sacsand
Copy link
Owner

sacsand commented Nov 14, 2021

Hi sorry for late reply. some-reason i didn't see this open issue.

You can try below method to access the context param 'user' and attribute values


type user struct{
	emailVerified bool
	userID, email string
}

func authController() fiber.Handler {
	return func(c *fiber.Ctx) error {
		
		currentUserInBytes := c.Locals("user").([]byte)

		var currentUser = new(user)
		err := json.Unmarshal(currentUserByte, &currentUser)
		if err != nil {
			return err
		}
		fmt.Println(currentUser)
		fmt.Println(currentUser.email)
		return nil

	}
}

thanks!

@Anthonyhawkins
Copy link
Author

Ah got it. Thanks for the help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants