Skip to content

Commit

Permalink
Merge pull request #759 from NUTFes/develop
Browse files Browse the repository at this point in the history
パスワードリセット機能と課程の変更対応
  • Loading branch information
Kubosaka authored May 29, 2024
2 parents 0891ea6 + 6d941ef commit 75355c5
Show file tree
Hide file tree
Showing 44 changed files with 1,338 additions and 317 deletions.
131 changes: 112 additions & 19 deletions api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1042,26 +1042,98 @@ const docTemplate = `{
}
},
},
"/fund_informations/details/{year}": {
"get": {
tags: ["fund_information"],
"description": "年度で指定されたfund_informationsに紐づくデータを取得",
"parameters": [
{
"name": "year",
"in": "path",
"description": "year",
"required": true,
"type": "integer"
}
],
"responses": {
"200": {
"description": "年度で指定されたfund_informationsに紐づくデータを取得",
}
"/fund_informations/details/{year}": {
"get": {
tags: ["fund_information"],
"description": "年度で指定されたfund_informationsに紐づくデータを取得",
"parameters": [
{
"name": "year",
"in": "path",
"description": "year",
"required": true,
"type": "integer"
}
},
},
],
"responses": {
"200": {
"description": "年度で指定されたfund_informationsに紐づくデータを取得",
}
}
},
},
"/password_reset/{id}": {
"post": {
tags: ["password_reset"],
"description": "password_変更",
responses: {
"200": {
"description": "passwordを変更する",
}
},
"parameters": [
{
"name": "id",
"in": "path",
"description": "id",
"required": true,
"type": "integer"
},
{
"in": "body",
"name": "passwordResetData",
"schema":{
"$ref": "#/definitions/passwordResetData"
},
},
],
},
},
"/password_reset/request": {
"post": {
tags: ["password_reset"],
"description": "password_reset_token発行リクエスト",
responses: {
"200": {
"description": "password_reset_tokenをメールアドレスに送信する",
}
},
"parameters": [
{
"name": "email",
"in": "query",
"description": "email",
"type": "string"
},
],
},
},
"/password_reset/{id}/valid": {
"post": {
tags: ["password_reset"],
"description": "トークンの称号",
responses: {
"200": {
"description": "password_reset_tokenが正しい確認する",
}
},
"parameters": [
{
"name": "id",
"in": "path",
"description": "id",
"required": true,
"type": "integer"
},
{
"name": "token",
"in": "query",
"description": "token",
"type": "string"
},
],
},
},
"/purchaseitems": {
"get": {
tags: ["purchase_item"],
Expand Down Expand Up @@ -2465,6 +2537,27 @@ const docTemplate = `{
"representative"
},
},
"passwordResetData":{
"properties":{
"token":{
"type": "string",
"example": "",
},
"password":{
"type": "string",
"example": "",
},
"confirmPassword":{
"type": "string",
"example": "",
},
},
"required":{
"year",
"startedAt",
"endedAt"
},
},
"purchaseReport":{
"properties":{
"userID":{
Expand Down
3 changes: 2 additions & 1 deletion api/externals/controller/mail_auth_controller.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package controller

import (
"net/http"

"github.com/NUTFes/FinanSu/api/internals/usecase"
"github.com/labstack/echo/v4"
"net/http"
)

type mailAuthController struct {
Expand Down
78 changes: 78 additions & 0 deletions api/externals/controller/password_reset_token_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package controller

import (
"errors"
"net/http"

"github.com/NUTFes/FinanSu/api/internals/domain"
"github.com/NUTFes/FinanSu/api/internals/usecase"
"github.com/labstack/echo/v4"
)

type passwordResetTokenController struct {
u usecase.PasswordResetTokenUseCase
}

type PasswordResetTokenController interface {
SendPasswordResetRequest(echo.Context) error
ValidPasswordResetToken(echo.Context) error
ChangePassword(echo.Context) error
}

func NewPasswordResetTokenController(u usecase.PasswordResetTokenUseCase) PasswordResetTokenController {
return &passwordResetTokenController{u}
}

// パスワード変更リクエスト
func (p *passwordResetTokenController) SendPasswordResetRequest(c echo.Context) error {
email := c.QueryParam("email")
err := p.u.PasswordResetTokenRequest(c.Request().Context(), email)
if err != nil {
return c.String(http.StatusOK, err.Error())
}
return c.String(http.StatusOK, "PasswordResetTokenを送信しました")
}

// トークンが有効チェック
func (p *passwordResetTokenController) ValidPasswordResetToken(c echo.Context) error {
id := c.Param("id")
token := c.QueryParam("token")
err := p.u.ValidPasswordResetToken(c.Request().Context(), id, token)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}

return c.String(http.StatusOK, "Tokenは有効です")
}


// パスワード変更
func (p *passwordResetTokenController) ChangePassword(c echo.Context) error {
id := c.Param("id")
passwordResetData := new(domain.PasswordResetData)

if err := c.Bind(passwordResetData); err != nil {
return err
}

//トークンの有効チェック
err := p.u.ValidPasswordResetToken(c.Request().Context(), id, passwordResetData.Token)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}

//パスワードのバリデーション
isValidPassword := passwordResetData.Password != passwordResetData.ConfirmPassword || passwordResetData.Password == "" || len(passwordResetData.Password)<6
if isValidPassword {
err = errors.New("パスワードが不正です")
return c.String(http.StatusBadRequest, err.Error())
}

//パスワード変更
err = p.u.ChangePassword(c.Request().Context(), id, passwordResetData.Password)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}

return c.String(http.StatusOK, "パスワードを変更しました")
}
16 changes: 13 additions & 3 deletions api/externals/repository/mail_auth_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@ package repository
import (
"context"
"database/sql"
"github.com/NUTFes/FinanSu/api/drivers/db"
"fmt"

"github.com/NUTFes/FinanSu/api/drivers/db"
"github.com/NUTFes/FinanSu/api/externals/repository/abstract"
)

type mailAuthRepository struct {
client db.Client
crud abstract.Crud
}

type MailAuthRepository interface {
CreateMailAuth(context.Context, string, string, string) (int64, error)
FindMailAuthByEmail(context.Context, string) *sql.Row
FindMailAuthByID(context.Context, string) *sql.Row
ChangePasswordByUserID(context.Context, string, string) error
}

func NewMailAuthRepository(client db.Client) MailAuthRepository {
return &mailAuthRepository{client}
func NewMailAuthRepository(client db.Client, crud abstract.Crud) MailAuthRepository {
return &mailAuthRepository{client, crud}
}

// 作成
Expand All @@ -43,3 +47,9 @@ func (r *mailAuthRepository) FindMailAuthByID(c context.Context, id string) *sql
fmt.Printf("\x1b[36m%s\n", query)
return row
}

// パスワードの変更
func (r *mailAuthRepository) ChangePasswordByUserID(c context.Context, userID string, password string) error {
query := "UPDATE mail_auth SET password = '"+ password +"' WHERE user_id = " + userID
return r.crud.UpdateDB(c, query)
}
Loading

0 comments on commit 75355c5

Please sign in to comment.