This repository has been archived by the owner on Dec 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: user registration (resolve #7)
- Loading branch information
Showing
14 changed files
with
238 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,8 @@ store: | |
path: 'data.gob.db' | ||
|
||
security: | ||
pepper: 'sshhh' | ||
pepper: 'sshhh' # Change this! | ||
allow_signup: false | ||
seed_users: | ||
- email: '[email protected]' | ||
password: 'admin' | ||
password: 'admin' # Change this! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.7.2 | ||
0.8.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"github.com/gorilla/mux" | ||
conf "github.com/muety/mailwhale/config" | ||
"github.com/muety/mailwhale/service" | ||
"github.com/muety/mailwhale/types" | ||
"github.com/muety/mailwhale/util" | ||
"github.com/muety/mailwhale/web/handlers" | ||
"net/http" | ||
) | ||
|
||
const routeUser = "/api/user" | ||
|
||
type UserHandler struct { | ||
config *conf.Config | ||
clientService *service.ClientService | ||
userService *service.UserService | ||
} | ||
|
||
func NewUserHandler() *UserHandler { | ||
return &UserHandler{ | ||
config: conf.Get(), | ||
clientService: service.NewClientService(), | ||
userService: service.NewUserService(), | ||
} | ||
} | ||
|
||
func (h *UserHandler) Register(router *mux.Router) { | ||
r := router.PathPrefix(routeUser).Subrouter() | ||
r.Path("").Methods(http.MethodPost).HandlerFunc(h.post) | ||
|
||
auth := handlers.NewAuthMiddleware(h.clientService, h.userService, []string{types.PermissionManageUser}) | ||
r2 := r.PathPrefix("").Subrouter() | ||
r2.Use(auth) | ||
|
||
r2.Path("/{id}").Methods(http.MethodPut).HandlerFunc(h.update) | ||
} | ||
|
||
func (h *UserHandler) post(w http.ResponseWriter, r *http.Request) { | ||
if !h.config.Security.AllowSignup { | ||
util.RespondErrorMessage(w, r, http.StatusMethodNotAllowed, errors.New("user registration is disabled on this server")) | ||
return | ||
} | ||
|
||
var payload types.Signup | ||
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { | ||
util.RespondError(w, r, http.StatusBadRequest, err) | ||
return | ||
} | ||
|
||
user, err := h.userService.Create(&payload) | ||
if err != nil { | ||
util.RespondError(w, r, http.StatusBadRequest, err) | ||
return | ||
} | ||
|
||
util.RespondJson(w, http.StatusCreated, user) | ||
} | ||
|
||
func (h *UserHandler) update(w http.ResponseWriter, r *http.Request) { | ||
reqClient := r.Context().Value(conf.KeyClient).(*types.Client) | ||
|
||
var payload types.Signup | ||
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { | ||
util.RespondError(w, r, http.StatusBadRequest, err) | ||
return | ||
} | ||
|
||
if payload.Email != reqClient.UserId { | ||
util.RespondEmpty(w, r, http.StatusForbidden) | ||
return | ||
} | ||
|
||
user, err := h.userService.GetById(reqClient.UserId) | ||
if err != nil { | ||
util.RespondError(w, r, http.StatusNotFound, err) | ||
return | ||
} | ||
|
||
user.Password = payload.Password | ||
|
||
user, err = h.userService.Update(user) | ||
if err != nil { | ||
util.RespondError(w, r, http.StatusInternalServerError, err) | ||
return | ||
} | ||
|
||
util.RespondJson(w, http.StatusOK, user) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { request } from './api' | ||
|
||
async function createUser(signup) { | ||
return (await request('/user', signup, { method: 'POST' })).data | ||
} | ||
|
||
async function updateUser(id, signup) { | ||
return (await request(`/user/${id}`, signup, { method: 'PUT' })).data | ||
} | ||
|
||
export { createUser, updateUser } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<script> | ||
import { createEventDispatcher } from 'svelte'; | ||
import { errors } from '../stores/alerts' | ||
const dispatch = createEventDispatcher() | ||
let username, password, passwordRepeat; | ||
function signup() { | ||
if (!username || !password || !passwordRepeat) return | ||
if (password !== passwordRepeat) { | ||
return errors.spawn('Passwords do not match') | ||
} | ||
dispatch('signup', { username, password }) | ||
} | ||
</script> | ||
|
||
<form class="flex flex-col w-full p-4 space-y-4" on:submit|preventDefault="{signup}"> | ||
<div class="flex flex-col w-full space-y-1"> | ||
<label for="email-input">E-Mail</label> | ||
<input | ||
type="email" | ||
class="p-2 border-2 rounded-md border-primary" | ||
name="email-input" | ||
placeholder="[email protected]" | ||
required | ||
bind:value={username} /> | ||
</div> | ||
|
||
<div class="flex flex-col w-full space-y-1"> | ||
<label for="password-input">Password</label> | ||
<input | ||
type="password" | ||
class="p-2 border-2 rounded-md border-primary" | ||
name="password-input" | ||
placeholder="********" | ||
required | ||
bind:value={password} /> | ||
</div> | ||
|
||
<div class="flex flex-col w-full space-y-1"> | ||
<label for="password-input">Password (repeat)</label> | ||
<input | ||
type="password" | ||
class="p-2 border-2 rounded-md border-primary" | ||
name="password-repeat-input" | ||
placeholder="********" | ||
required | ||
bind:value={passwordRepeat} /> | ||
</div> | ||
|
||
<div class="flex justify-between py-2"> | ||
<div /> | ||
<button | ||
type="submit" | ||
class="px-4 py-2 text-white rounded-md bg-primary hover:bg-primary-dark">Sign Up</button> | ||
</div> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.