update/fix/admin-and-student-retrieve-student-data
This commit is contained in:
1 parent
7c178de3f0
commit
90b69bbdcc
3 files changed
+32
-2
No files matched your search
@@ -636,11 +636,33 @@ func AdminLogin(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Student(c *fiber.Ctx) error {
|
func Student(c *fiber.Ctx) error {
|
||||||
|
var sid string
|
||||||
|
if AuthAdmin(c) {
|
||||||
|
var data map[string]string
|
||||||
|
|
||||||
|
if err := c.BodyParser(&data); err != nil {
|
||||||
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
|
"success": false,
|
||||||
|
"message": "Failed to parse body",
|
||||||
|
"error": err,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check required fields are included
|
||||||
|
if data["sid"] == "" {
|
||||||
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||||
|
"success": false,
|
||||||
|
"message": "missing required fields",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
sid = data["sid"]
|
||||||
|
} else {
|
||||||
cookie := c.Cookies("jwt")
|
cookie := c.Cookies("jwt")
|
||||||
|
|
||||||
token, err := jwt.ParseWithClaims(cookie, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
|
token, err := jwt.ParseWithClaims(cookie, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||||
return []byte(SecretKey), nil
|
return []byte(SecretKey), nil
|
||||||
})
|
})
|
||||||
|
// This returns not authorized for both admin and student
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
@@ -649,11 +671,16 @@ func Student(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
claims := token.Claims.(*jwt.StandardClaims)
|
claims := token.Claims.(*jwt.StandardClaims)
|
||||||
|
sid = claims.Issuer
|
||||||
|
}
|
||||||
|
|
||||||
var responceData map[string]interface{}
|
responceData := make(map[string]interface{})
|
||||||
|
responceData["student"] = nil
|
||||||
|
responceData["locker"] = nil
|
||||||
|
responceData["contacts"] = nil
|
||||||
|
|
||||||
var student models.Student
|
var student models.Student
|
||||||
findErr := studentCollection.FindOne(context.TODO(), bson.M{"schooldata.sid": claims.Issuer}).Decode(&student)
|
findErr := studentCollection.FindOne(context.TODO(), bson.M{"schooldata.sid": sid}).Decode(&student)
|
||||||
if findErr != nil {
|
if findErr != nil {
|
||||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ go 1.16
|
|||||||
require (
|
require (
|
||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
||||||
github.com/gofiber/fiber/v2 v2.19.0
|
github.com/gofiber/fiber/v2 v2.19.0
|
||||||
|
github.com/google/uuid v1.3.0
|
||||||
github.com/joho/godotenv v1.3.0
|
github.com/joho/godotenv v1.3.0
|
||||||
go.mongodb.org/mongo-driver v1.7.2
|
go.mongodb.org/mongo-driver v1.7.2
|
||||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
|
||||||
|
|||||||
@@ -39,6 +39,8 @@ github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA=
|
|||||||
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||||
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
|
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
|
||||||
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||||
|
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||||
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
|
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
|
||||||
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
|
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
|
||||||
|
|||||||
Reference in new issue
Block a user