Login and main router with jwt tokens
This commit is contained in:
1 parent
7049abf5d8
commit
0a357a7c2b
6 files changed
+219
-26
No files matched your search
+199
-20
@@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/dgrijalva/jwt-go"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
@@ -17,6 +18,8 @@ import (
|
|||||||
var teacherCollection *mongo.Collection = database.OpenCollection(database.Client, "teachers")
|
var teacherCollection *mongo.Collection = database.OpenCollection(database.Client, "teachers")
|
||||||
var studentCollection *mongo.Collection = database.OpenCollection(database.Client, "students")
|
var studentCollection *mongo.Collection = database.OpenCollection(database.Client, "students")
|
||||||
|
|
||||||
|
const SecretKey = "secret"
|
||||||
|
|
||||||
func Enroll(c *fiber.Ctx) error {
|
func Enroll(c *fiber.Ctx) error {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
|
|
||||||
@@ -24,7 +27,7 @@ func Enroll(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
if err := c.BodyParser(&data); err != nil {
|
if err := c.BodyParser(&data); err != nil {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(400).JSON(fiber.Map{
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "Failed to parse body",
|
"message": "Failed to parse body",
|
||||||
"error": err,
|
"error": err,
|
||||||
@@ -34,7 +37,7 @@ func Enroll(c *fiber.Ctx) error {
|
|||||||
// Check minimum enroll field requirements are met
|
// Check minimum enroll field requirements are met
|
||||||
if data["firstname"] == "" || data["lastname"] == "" || data["age"] == "" || data["gradelevel"] == "" || data["dob"] == "" || data["email"] == "" {
|
if data["firstname"] == "" || data["lastname"] == "" || data["age"] == "" || data["gradelevel"] == "" || data["dob"] == "" || data["email"] == "" {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(400).JSON(fiber.Map{
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "missing required fields",
|
"message": "missing required fields",
|
||||||
})
|
})
|
||||||
@@ -73,7 +76,7 @@ func Enroll(c *fiber.Ctx) error {
|
|||||||
result, insertErr := studentCollection.InsertOne(ctx, student)
|
result, insertErr := studentCollection.InsertOne(ctx, student)
|
||||||
if insertErr != nil {
|
if insertErr != nil {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(400).JSON(fiber.Map{
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "the student could not be inserted",
|
"message": "the student could not be inserted",
|
||||||
"error": insertErr,
|
"error": insertErr,
|
||||||
@@ -81,7 +84,11 @@ func Enroll(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
return c.Status(201).JSON(result)
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||||
|
"success": true,
|
||||||
|
"message": "successfully inserted student",
|
||||||
|
"result": result,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterTeacher(c *fiber.Ctx) error {
|
func RegisterTeacher(c *fiber.Ctx) error {
|
||||||
@@ -91,7 +98,7 @@ func RegisterTeacher(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
if err := c.BodyParser(&data); err != nil {
|
if err := c.BodyParser(&data); err != nil {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(400).JSON(fiber.Map{
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "Failed to parse body",
|
"message": "Failed to parse body",
|
||||||
"error": err,
|
"error": err,
|
||||||
@@ -101,7 +108,7 @@ func RegisterTeacher(c *fiber.Ctx) error {
|
|||||||
// Check minimum register teacher field requirements are met
|
// Check minimum register teacher field requirements are met
|
||||||
if data["firstname"] == "" || data["lastname"] == "" || data["dob"] == "" || data["email"] == "" {
|
if data["firstname"] == "" || data["lastname"] == "" || data["dob"] == "" || data["email"] == "" {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(400).JSON(fiber.Map{
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "missing required fields",
|
"message": "missing required fields",
|
||||||
})
|
})
|
||||||
@@ -126,7 +133,7 @@ func RegisterTeacher(c *fiber.Ctx) error {
|
|||||||
result, insertErr := teacherCollection.InsertOne(ctx, teacher)
|
result, insertErr := teacherCollection.InsertOne(ctx, teacher)
|
||||||
if insertErr != nil {
|
if insertErr != nil {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(400).JSON(fiber.Map{
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "the teacher could not be inserted",
|
"message": "the teacher could not be inserted",
|
||||||
"error": insertErr,
|
"error": insertErr,
|
||||||
@@ -134,7 +141,116 @@ func RegisterTeacher(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
return c.Status(201).JSON(result)
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
||||||
|
"success": true,
|
||||||
|
"message": "successfully inserted teacher",
|
||||||
|
"result": result,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func StudentLogin(c *fiber.Ctx) error {
|
||||||
|
var data map[string]string
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
|
|
||||||
|
if err := c.BodyParser(&data); err != nil {
|
||||||
|
cancel()
|
||||||
|
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"] == "" || data["password"] == "" {
|
||||||
|
cancel()
|
||||||
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||||
|
"success": false,
|
||||||
|
"message": "missing required fields",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
var student models.Student
|
||||||
|
err := studentCollection.FindOne(ctx, bson.M{"sid": data["sid"]}).Decode(&student)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
cancel()
|
||||||
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
|
"success": false,
|
||||||
|
"message": "student not found",
|
||||||
|
"error": err,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
var verified bool = student.ComparePasswords(data["password"])
|
||||||
|
if verified == false {
|
||||||
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||||
|
"success": false,
|
||||||
|
"message": "incorrect password",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
claims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.StandardClaims{
|
||||||
|
Issuer: student.SID,
|
||||||
|
ExpiresAt: time.Now().Add(time.Hour * 24).Unix(), // 1 Day
|
||||||
|
})
|
||||||
|
token, err := claims.SignedString([]byte(SecretKey))
|
||||||
|
if err != nil {
|
||||||
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
|
"success": false,
|
||||||
|
"message": "could not log in",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
cookie := fiber.Cookie{
|
||||||
|
Name: "jwt",
|
||||||
|
Value: token,
|
||||||
|
Expires: time.Now().Add(time.Hour * 24),
|
||||||
|
HTTPOnly: true,
|
||||||
|
}
|
||||||
|
c.Cookie(&cookie)
|
||||||
|
|
||||||
|
return c.Status(200).JSON(fiber.Map{
|
||||||
|
"success": true,
|
||||||
|
"message": "correct password",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TeacherLogin(c *fiber.Ctx) error {
|
||||||
|
return c.JSON("status:ok")
|
||||||
|
}
|
||||||
|
|
||||||
|
func Student(c *fiber.Ctx) error {
|
||||||
|
cookie := c.Cookies("jwt")
|
||||||
|
|
||||||
|
token, err := jwt.ParseWithClaims(cookie, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||||
|
return []byte(SecretKey), nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||||
|
"success": false,
|
||||||
|
"message": "student not found",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
claims := token.Claims.(*jwt.StandardClaims)
|
||||||
|
|
||||||
|
var student models.Student
|
||||||
|
findErr := studentCollection.FindOne(context.TODO(), bson.M{"sid": claims.Issuer}).Decode(&student)
|
||||||
|
if findErr != nil {
|
||||||
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||||
|
"success": false,
|
||||||
|
"message": "student not found",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(student)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Teacher(c *fiber.Ctx) error {
|
||||||
|
return c.JSON("status:ok")
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateStudentName(c *fiber.Ctx) error {
|
func UpdateStudentName(c *fiber.Ctx) error {
|
||||||
@@ -143,7 +259,7 @@ func UpdateStudentName(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
if err := c.BodyParser(&data); err != nil {
|
if err := c.BodyParser(&data); err != nil {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(400).JSON(fiber.Map{
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "Failed to parse body",
|
"message": "Failed to parse body",
|
||||||
"error": err,
|
"error": err,
|
||||||
@@ -153,7 +269,7 @@ func UpdateStudentName(c *fiber.Ctx) error {
|
|||||||
// Check id and names are included
|
// Check id and names are included
|
||||||
if data["_id"] == "" || data["firstname"] == "" || data["middlename"] == "" || data["lastname"] == "" {
|
if data["_id"] == "" || data["firstname"] == "" || data["middlename"] == "" || data["lastname"] == "" {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(400).JSON(fiber.Map{
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "missing required fields",
|
"message": "missing required fields",
|
||||||
})
|
})
|
||||||
@@ -164,7 +280,7 @@ func UpdateStudentName(c *fiber.Ctx) error {
|
|||||||
cancel()
|
cancel()
|
||||||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
|
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "Catchphrase not found",
|
"message": "student not found",
|
||||||
"error": err,
|
"error": err,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -183,7 +299,7 @@ func UpdateStudentName(c *fiber.Ctx) error {
|
|||||||
)
|
)
|
||||||
if updateErr != nil {
|
if updateErr != nil {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(400).JSON(fiber.Map{
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "the student could not be updated",
|
"message": "the student could not be updated",
|
||||||
"error": updateErr,
|
"error": updateErr,
|
||||||
@@ -191,11 +307,70 @@ func UpdateStudentName(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
return c.Status(200).JSON(result)
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
||||||
|
"success": true,
|
||||||
|
"message": "successfully updated student",
|
||||||
|
"result": result,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateStudentGradeLevel(c *fiber.Ctx) error {
|
func UpdateStudentGradeLevel(c *fiber.Ctx) error {
|
||||||
return c.JSON("status:ok")
|
var data map[string]string
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
|
|
||||||
|
if err := c.BodyParser(&data); err != nil {
|
||||||
|
cancel()
|
||||||
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
|
"success": false,
|
||||||
|
"message": "Failed to parse body",
|
||||||
|
"error": err,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check required fields are included
|
||||||
|
if data["_id"] == "" || data["gradelevel"] == "" {
|
||||||
|
cancel()
|
||||||
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||||
|
"success": false,
|
||||||
|
"message": "missing required fields",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
studentObjectId, err := primitive.ObjectIDFromHex(data["_id"])
|
||||||
|
if err != nil {
|
||||||
|
cancel()
|
||||||
|
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
|
||||||
|
"success": false,
|
||||||
|
"message": "student not found",
|
||||||
|
"error": err,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
update := bson.M{
|
||||||
|
"$set": bson.M{
|
||||||
|
"gradelevel": data["gradelevel"],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
result, updateErr := studentCollection.UpdateOne(
|
||||||
|
ctx,
|
||||||
|
bson.M{"_id": studentObjectId},
|
||||||
|
update,
|
||||||
|
)
|
||||||
|
if updateErr != nil {
|
||||||
|
cancel()
|
||||||
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
|
"success": false,
|
||||||
|
"message": "the student could not be updated",
|
||||||
|
"error": updateErr,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
||||||
|
"success": true,
|
||||||
|
"message": "successfully updated student",
|
||||||
|
"result": result,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateStudentHomeroom(c *fiber.Ctx) error {
|
func UpdateStudentHomeroom(c *fiber.Ctx) error {
|
||||||
@@ -256,7 +431,7 @@ func UpdateTeacherName(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
if err := c.BodyParser(&data); err != nil {
|
if err := c.BodyParser(&data); err != nil {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(400).JSON(fiber.Map{
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "Failed to parse body",
|
"message": "Failed to parse body",
|
||||||
"error": err,
|
"error": err,
|
||||||
@@ -266,7 +441,7 @@ func UpdateTeacherName(c *fiber.Ctx) error {
|
|||||||
// Check id and names are included
|
// Check id and names are included
|
||||||
if data["_id"] == "" || data["firstname"] == "" || data["middlename"] == "" || data["lastname"] == "" {
|
if data["_id"] == "" || data["firstname"] == "" || data["middlename"] == "" || data["lastname"] == "" {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(400).JSON(fiber.Map{
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "missing required fields",
|
"message": "missing required fields",
|
||||||
})
|
})
|
||||||
@@ -277,7 +452,7 @@ func UpdateTeacherName(c *fiber.Ctx) error {
|
|||||||
cancel()
|
cancel()
|
||||||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
|
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "Catchphrase not found",
|
"message": "teacher not found",
|
||||||
"error": err,
|
"error": err,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -296,13 +471,17 @@ func UpdateTeacherName(c *fiber.Ctx) error {
|
|||||||
)
|
)
|
||||||
if updateErr != nil {
|
if updateErr != nil {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(400).JSON(fiber.Map{
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "the student could not be updated",
|
"message": "the teacher could not be updated",
|
||||||
"error": updateErr,
|
"error": updateErr,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
return c.Status(200).JSON(result)
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
||||||
|
"success": true,
|
||||||
|
"message": "successfully updated teacher",
|
||||||
|
"result": result,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,7 @@ module school-management
|
|||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
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/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
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu
|
|||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
|
||||||
|
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||||
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
|
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
|
||||||
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
||||||
github.com/gobuffalo/attrs v0.0.0-20190224210810-a9411de4debd/go.mod h1:4duuawTqi2wkkpB4ePgWMaai6/Kc6WEz83bhFwpHzj0=
|
github.com/gobuffalo/attrs v0.0.0-20190224210810-a9411de4debd/go.mod h1:4duuawTqi2wkkpB4ePgWMaai6/Kc6WEz83bhFwpHzj0=
|
||||||
|
|||||||
@@ -1,19 +1,19 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"school-management/database"
|
|
||||||
|
|
||||||
"school-management/routes"
|
"school-management/routes"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||||
)
|
)
|
||||||
|
|
||||||
var studentCollection *mongo.Collection = database.OpenCollection(database.Client, "students")
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := fiber.New()
|
app := fiber.New()
|
||||||
|
|
||||||
|
app.Use(cors.New(cors.Config{
|
||||||
|
AllowCredentials: true,
|
||||||
|
}))
|
||||||
|
|
||||||
routes.Setup(app)
|
routes.Setup(app)
|
||||||
|
|
||||||
app.Listen(":8000")
|
app.Listen(":8000")
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ type Student struct {
|
|||||||
GradeLevel int `json:"gradelevel" validate:"required"`
|
GradeLevel int `json:"gradelevel" validate:"required"`
|
||||||
Email string `json:"email" validate:"required"`
|
Email string `json:"email" validate:"required"`
|
||||||
SchoolEmail string `json:"schoolemail"`
|
SchoolEmail string `json:"schoolemail"`
|
||||||
Password string `json:"password" validate:"min=10,max=32"`
|
Password string `json:"-" validate:"min=10,max=32"`
|
||||||
TempPassword bool `json:"temppassword"`
|
TempPassword bool `json:"temppassword"`
|
||||||
SID string `json:"sid"` // Student ID
|
SID string `json:"sid"` // Student ID
|
||||||
PEN string `json:"ped"` // Personal Education Number
|
PEN string `json:"ped"` // Personal Education Number
|
||||||
|
|||||||
@@ -13,7 +13,13 @@ type Person struct {
|
|||||||
|
|
||||||
func Setup(app *fiber.App) {
|
func Setup(app *fiber.App) {
|
||||||
// API Handling
|
// API Handling
|
||||||
|
|
||||||
|
// Login + Register handling
|
||||||
|
app.Get("/api/student", controllers.Student)
|
||||||
app.Post("/api/student/enroll", controllers.Enroll)
|
app.Post("/api/student/enroll", controllers.Enroll)
|
||||||
|
app.Post("/api/student/login", controllers.StudentLogin)
|
||||||
|
|
||||||
|
// Updating handling
|
||||||
app.Post("/api/student/updateName", controllers.UpdateStudentName)
|
app.Post("/api/student/updateName", controllers.UpdateStudentName)
|
||||||
app.Post("/api/student/updateGradeLevel", controllers.UpdateStudentGradeLevel)
|
app.Post("/api/student/updateGradeLevel", controllers.UpdateStudentGradeLevel)
|
||||||
app.Post("/api/student/updateHomeroom", controllers.UpdateStudentHomeroom)
|
app.Post("/api/student/updateHomeroom", controllers.UpdateStudentHomeroom)
|
||||||
@@ -25,7 +31,12 @@ func Setup(app *fiber.App) {
|
|||||||
app.Post("/api/student/updatePhoto", controllers.UpdateStudentPhoto)
|
app.Post("/api/student/updatePhoto", controllers.UpdateStudentPhoto)
|
||||||
app.Post("/api/student/updateEmail", controllers.UpdateStudentEmail)
|
app.Post("/api/student/updateEmail", controllers.UpdateStudentEmail)
|
||||||
|
|
||||||
|
// Login + Register handling
|
||||||
|
app.Get("/api/teacher", controllers.Teacher)
|
||||||
app.Post("/api/teacher/register", controllers.RegisterTeacher)
|
app.Post("/api/teacher/register", controllers.RegisterTeacher)
|
||||||
|
app.Post("/api/teacher/login", controllers.TeacherLogin)
|
||||||
|
|
||||||
|
// Updating handling
|
||||||
app.Post("/api/teacher/updatePassword", controllers.UpdateTeacherPassword)
|
app.Post("/api/teacher/updatePassword", controllers.UpdateTeacherPassword)
|
||||||
app.Post("/api/teacher/updateAddress", controllers.UpdateTeacherAddress)
|
app.Post("/api/teacher/updateAddress", controllers.UpdateTeacherAddress)
|
||||||
app.Post("/api/teacher/updatePhoto", controllers.UpdateTeacherPhoto)
|
app.Post("/api/teacher/updatePhoto", controllers.UpdateTeacherPhoto)
|
||||||
|
|||||||
Reference in new issue
Block a user