summaryrefslogtreecommitdiff
path: root/pkg/web/register.go
blob: 8ccf6a90b15a6d5e7d10a97c2b9e8fc7717283f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package web

import (
	"encoding/json"
	"fmt"
	"net/http"

	"mokhan.ca/xlgmokha/oauth/pkg/dto"
	"mokhan.ca/xlgmokha/oauth/pkg/tasks"
)

func (h *HttpContext) Register(w http.ResponseWriter, r *http.Request) {
	var request dto.ClientRegistrationRequest
	json.NewDecoder(r.Body).Decode(&request)

	if response, err := tasks.CreateClient(request); err != nil {
		w.WriteHeader(http.StatusBadRequest)
		w.Header().Set("Content-Type", "application/json")
		w.Header().Set("Cache-Control", "no-store")
		w.Header().Set("Pragma", "no-cache")
		fmt.Fprintf(w, `{"error":"%s","error_description":"%s"}`, err.Error(), err.Error())
	} else {
		w.WriteHeader(http.StatusCreated)
		w.Header().Set("Content-Type", "application/json")
		w.Header().Set("Cache-Control", "no-store")
		w.Header().Set("Pragma", "no-cache")
		json.NewEncoder(w).Encode(response)
	}
}