Beispiel #1
0
func Logout(c *echo.Context) error {
	a, ok := c.Get("user").(models.Account)
	if !ok {
		logrus.Errorf("failed to get user in logout")
		c.JSON(500, Response{})
		return nil
	}

	db := c.Get("db").(*mgo.Database)
	err := a.ClearSession(db)
	if err != nil {
		logrus.Errorf("failed to clear session in logout: %s", err.Error())
		c.JSON(500, Response{})
		return nil
	}

	if err := models.RemovePlayerFromGames(db, a.AccountID); err != nil {
		logrus.Errorf("failed to remove player from games in logout: %s", err.Error())
		c.JSON(500, Response{})
		return nil
	}

	c.JSON(200, Response{Success: true})
	return nil
}
Beispiel #2
0
func Login(c *echo.Context) error {
	logrus.Infof("login")

	loginRequest, err := validateLogin(c.Request().Body)
	if err != nil {
		logrus.Errorf("failed login validation: %s", err.Error())
		c.JSON(400, Response{})
		return nil
	}

	db := c.Get("db").(*mgo.Database)
	account, err := models.LoadAccount(db, loginRequest.Username)
	if err != nil {
		logrus.Errorf("failed to load account in login: %s", err)
		c.JSON(500, Response{})
		return nil
	}

	if err := bcrypt.CompareHashAndPassword([]byte(account.Hashword), []byte(loginRequest.Password)); err != nil {
		logrus.Errorf("failed to authenticate in login: %s", err.Error())
		c.JSON(401, Response{})
		return nil
	}

	sessionID, err := account.NewSession(db)
	if err != nil {
		logrus.Errorf("failed to create new session in login: %s", err.Error())
		c.JSON(500, Response{})
		return nil
	}

	if err := models.RemovePlayerFromGames(db, account.AccountID); err != nil {
		logrus.Errorf("failed to remove player from games in login: %s", err.Error())
		c.JSON(500, Response{})
		return nil
	}

	resp := struct {
		SessionID string `json:"sessionID"`
		AccountID string `json:"accountID"`
		Username  string `json:"username"`
	}{sessionID, account.AccountID, account.Username}

	c.JSON(200, Response{
		Success: true,
		Payload: resp,
	})
	return nil
}