I have managed to use the local context to set database query results using a custom middleware. I am trying to see how I can authenticate a user then pull their details from the database and inject it to the context. This has been done.
The local data on the final method on the route is actually an interface and I would like to extract fields from the data I set from the previous auth middleware. How can I work with this interface type to some form like a struct or json so that I can get the fields and values for doing some logic?
user := c.Locals("user")
fmt.Println("checking for locals",user)
From above user is of struct user
{
Name string `json:"name"`
Emain string `json:"email"`
ID string `json:"id"`
Password string `json:"password"`
}
How can I get the name and email ?
CodePudding user response:
So after digging into the fiber docs and reading about interfaces and especially empty interfaces I think I have a solution but stand to be corrected
I saw one can cast an interface to a concrete type. In my case I would take the c.Locals("user")
of type var user interface{}
then cast it to a struct passing the pointer to the user model as follows
user := c.Locals("user")
details, ok :=user.(*models.User)
fmt.Println("checking for locals -----------",details.Name)