Skip to content

Commit

Permalink
feat: add API to generate FB custom token from Kakao
Browse files Browse the repository at this point in the history
  • Loading branch information
litsynp committed Jan 30, 2024
1 parent 7c9f97b commit efcd740
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 11 deletions.
39 changes: 39 additions & 0 deletions cmd/server/handler/auth_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,42 @@ func (h *authHandler) KakaoCallback(w http.ResponseWriter, r *http.Request) {
PhotoURL: userProfile.Properties.ProfileImage,
})
}

// GenerateFBCustomTokenFromKakao godoc
// @Summary Kakao OAuth 토큰 기반 Firebase Custom Token 생성 API
// @Description 주어진 카카오 토큰으로 사용자 기본 정보를 검증하고 Firebase Custom Token을 발급합니다.
// @Tags auth
// @Accept json
// @Produce json
// @Param request body auth.GenerateFBCustomTokenRequest true "Firebase Custom Token 생성 요청"
// @Success 201 {object} auth.GenerateFBCustomTokenResponse
// @Failure 400 {object} commonviews.ErrorView
// @Router /auth/custom-tokens/kakao [post]
func (h *authHandler) GenerateFBCustomTokenFromKakao(w http.ResponseWriter, r *http.Request) {
var tokenRequest auth.GenerateFBCustomTokenRequest
if err := commonviews.ParseBody(w, r, &tokenRequest); err != nil {
return
}

userProfile, err := h.kakaoClient.FetchUserProfile(tokenRequest.OAuthToken)
if err != nil {
commonviews.BadRequest(w, nil, "유효하지 않은 Kakao 인증 정보입니다.")
return
}

customToken, err := h.authService.CustomToken(r.Context(), fmt.Sprintf("%d", userProfile.ID))
if err != nil {
commonviews.InternalServerError(w, nil, err.Error())
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(auth.GenerateFBCustomTokenResponse{
AuthToken: customToken,
FirebaseProviderType: user.FirebaseProviderTypeKakao,
FirebaseUID: fmt.Sprintf("%d", userProfile.ID),
Email: userProfile.KakaoAccount.Email,
PhotoURL: userProfile.Properties.ProfileImage,
})
}
2 changes: 1 addition & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// @title 이웃집멍냥 API 문서
// @version 0.7.0
// @version 0.8.0
// @description 이웃집멍냥 백엔드 API 문서입니다.
// @termsOfService http://swagger.io/terms/

Expand Down
1 change: 1 addition & 0 deletions cmd/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func NewRouter(app *firebaseinfra.FirebaseApp) *chi.Mux {
r.Route("/auth", func(r chi.Router) {
r.Get("/login/kakao", authHandler.KakaoLogin)
r.Get("/callback/kakao", authHandler.KakaoCallback)
r.Post("/custom-tokens/kakao", authHandler.GenerateFBCustomTokenFromKakao)
})
r.Route("/media", func(r chi.Router) {
r.Get("/{id}", mediaHandler.FindMediaByID)
Expand Down
16 changes: 16 additions & 0 deletions internal/domain/auth/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,19 @@ type KakaoCallbackResponse struct {
Email string `json:"email"`
PhotoURL string `json:"photoURL"`
}

// GenerateFBCustomTokenRequest는 OAuth 토큰 정보를 기반으로 Firebase Custom Token을 생성하기 위한 요청이다.
type GenerateFBCustomTokenRequest struct {
OAuthToken string `json:"oauthToken"`
OAuthID string `json:"oauthId"`
OAuthEmail string `json:"oauthEmail"`
}

// GenerateFBCustomTokenResponse는 Firebase Custom Token을 생성하기 위한 응답이다.
type GenerateFBCustomTokenResponse struct {
AuthToken string `json:"authToken"`
FirebaseProviderType user.FirebaseProviderType `json:"fbProviderType"`
FirebaseUID string `json:"fbUid"`
Email string `json:"email"`
PhotoURL string `json:"photoURL"`
}
3 changes: 2 additions & 1 deletion internal/infra/kakao/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kakaoinfra

import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
Expand Down Expand Up @@ -70,7 +71,7 @@ func (kakaoClient *KakaoClient) FetchUserProfile(code string) (*kakaoUserProfile
}

if res.StatusCode != http.StatusOK {
return nil, err
return nil, fmt.Errorf("failed to fetch user profile from Kakao server")
}

body, err := io.ReadAll(res.Body)
Expand Down
90 changes: 86 additions & 4 deletions pkg/docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 84 additions & 3 deletions pkg/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "0.7.0"
"version": "0.8.0"
},
"basePath": "/api",
"paths": {
Expand All @@ -33,6 +33,46 @@
}
}
},
"/auth/custom-tokens/kakao": {
"post": {
"description": "주어진 카카오 토큰으로 사용자 기본 정보를 검증하고 Firebase Custom Token을 발급합니다.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"auth"
],
"summary": "Kakao OAuth 토큰 기반 Firebase Custom Token 생성 API",
"parameters": [
{
"description": "Firebase Custom Token 생성 요청",
"name": "request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/auth.GenerateFBCustomTokenRequest"
}
}
],
"responses": {
"201": {
"description": "Created",
"schema": {
"$ref": "#/definitions/auth.GenerateFBCustomTokenResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/commonviews.ErrorView"
}
}
}
}
},
"/auth/login/kakao": {
"get": {
"tags": [
Expand Down Expand Up @@ -596,6 +636,40 @@
}
},
"definitions": {
"auth.GenerateFBCustomTokenRequest": {
"type": "object",
"properties": {
"oauthEmail": {
"type": "string"
},
"oauthId": {
"type": "string"
},
"oauthToken": {
"type": "string"
}
}
},
"auth.GenerateFBCustomTokenResponse": {
"type": "object",
"properties": {
"authToken": {
"type": "string"
},
"email": {
"type": "string"
},
"fbProviderType": {
"$ref": "#/definitions/user.FirebaseProviderType"
},
"fbUid": {
"type": "string"
},
"photoURL": {
"type": "string"
}
}
},
"auth.KakaoCallbackResponse": {
"type": "object",
"properties": {
Expand All @@ -616,6 +690,14 @@
}
}
},
"commonviews.ErrorView": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
}
},
"commonviews.PaginatedView-pet_BreedView": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -1258,8 +1340,7 @@
"fbProviderType",
"fbUid",
"fullname",
"nickname",
"profileImageId"
"nickname"
],
"properties": {
"email": {
Expand Down
Loading

0 comments on commit efcd740

Please sign in to comment.