Proper login endpoint functionality
This commit is contained in:
		
							parent
							
								
									c6d9307979
								
							
						
					
					
						commit
						c13378d3b9
					
				
					 2 changed files with 11 additions and 2 deletions
				
			
		|  | @ -14,6 +14,7 @@ import ( | ||||||
| type Database interface { | type Database interface { | ||||||
| 	// Insert a new user into the database, password should be hashed before calling | 	// Insert a new user into the database, password should be hashed before calling | ||||||
| 	AddUser(username string, password string) error | 	AddUser(username string, password string) error | ||||||
|  | 	CheckUser(username string, password string) bool | ||||||
| 	RemoveUser(username string) error | 	RemoveUser(username string) error | ||||||
| 	PromoteToAdmin(username string) error | 	PromoteToAdmin(username string) error | ||||||
| 	GetUserId(username string) (int, error) | 	GetUserId(username string) (int, error) | ||||||
|  | @ -77,6 +78,15 @@ func DbConnect(dbpath string) Database { | ||||||
| 	return &Db{db} | 	return &Db{db} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func (d *Db) CheckUser(username string, password string) bool { | ||||||
|  | 	var dbPassword string | ||||||
|  | 	err := d.Get(&dbPassword, "SELECT password FROM users WHERE username = ?", username) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return false | ||||||
|  | 	} | ||||||
|  | 	return dbPassword == password | ||||||
|  | } | ||||||
|  | 
 | ||||||
| // GetProjectsForUser retrieves all projects associated with a specific user. | // GetProjectsForUser retrieves all projects associated with a specific user. | ||||||
| func (d *Db) GetProjectsForUser(username string) ([]types.Project, error) { | func (d *Db) GetProjectsForUser(username string) ([]types.Project, error) { | ||||||
| 	var projects []types.Project | 	var projects []types.Project | ||||||
|  |  | ||||||
|  | @ -110,8 +110,7 @@ func (gs *GState) Login(c *fiber.Ctx) error { | ||||||
| 	user := c.FormValue("user") | 	user := c.FormValue("user") | ||||||
| 	pass := c.FormValue("pass") | 	pass := c.FormValue("pass") | ||||||
| 
 | 
 | ||||||
| 	// Throws Unauthorized error | 	if !gs.Db.CheckUser(user, pass) { | ||||||
| 	if user != "user" || pass != "pass" { |  | ||||||
| 		return c.SendStatus(fiber.StatusUnauthorized) | 		return c.SendStatus(fiber.StatusUnauthorized) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Imbus
						Imbus