-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f504fa3
commit 62ad93e
Showing
8 changed files
with
134 additions
and
18 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
37 changes: 37 additions & 0 deletions
37
backend/src/swlkup/resolver/root/ngo/supervisor_password_reset.clj
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,37 @@ | ||
(ns swlkup.resolver.root.ngo.supervisor-password-reset | ||
(:require [clojure.spec.alpha :as s] | ||
[specialist-server.type :as t] | ||
[swlkup.auth.core :refer [auth+role->entity]] | ||
[swlkup.model.auth :as auth] | ||
[swlkup.model.ngo :as ngo] | ||
[swlkup.model.login :as login] | ||
[swlkup.auth.password.generate :refer [generate-password]] | ||
[swlkup.auth.password.hash :refer [hash-password]])) | ||
|
||
(s/def ::supervisor t/string) | ||
|
||
(s/fdef supervisor_password_reset | ||
:args (s/tuple map? (s/keys :req-un [::auth/auth ::supervisor]) map? map?) | ||
:ret (s/keys :req-un [::login/mail ::login/password])) | ||
|
||
(defn supervisor_password_reset | ||
[_node opt ctx _info] | ||
(let [{:keys [q_id_unary tx_sync, tx-committed?]} (:db_ctx ctx) | ||
[ngo:id] (auth+role->entity ctx (:auth opt) ::ngo/doc) | ||
supervisor (:supervisor opt)] | ||
(when ngo:id | ||
(let [login (q_id_unary '{:find [(pull ?l [*])] | ||
:where [[?l :xt/spec ::login/doc] | ||
[?l :invited-by ?ngo:id] | ||
[?l :mail ?mail]] | ||
:in [[?ngo:id ?mail]]} | ||
[ngo:id supervisor]) | ||
password (generate-password) | ||
password-hash (hash-password password) | ||
t (when login | ||
(tx_sync [[:xtdb.api/put (assoc login | ||
:password-hash password-hash)]]))] | ||
(when (tx-committed? t) | ||
{:mail (:mail login) :password password}))))) | ||
|
||
(s/def ::supervisor_password_reset (t/resolver #'supervisor_password_reset)) |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { useAuthStore, AuthState } from '../Login' | ||
import { fetcher } from '../../codegen/fetcher' | ||
import { useTranslation } from 'react-i18next'; | ||
import create from 'zustand' | ||
|
||
type SupervisorMail = string; | ||
type SupervisorPassword = string; | ||
|
||
interface PasswortMap { | ||
[Key: SupervisorMail]: SupervisorPassword; | ||
} | ||
|
||
export interface PasswortMapState { | ||
passwortMap: PasswortMap, | ||
addPasswort: (mail: SupervisorMail, password: SupervisorPassword) => void, | ||
} | ||
|
||
export const usePasswortMapStore = create<PasswortMapState>(set => ({ | ||
passwortMap: {}, | ||
addPasswort: (mail, password) => set( orig => ({passwortMap: {...orig.passwortMap, [mail]: password}})), | ||
})) | ||
|
||
async function pwReset (auth: AuthState, supervisor: string, addPasswort: PasswortMapState["addPasswort"]) { | ||
const result = await fetcher<any, any>(`mutation SupervisorPasswordReset($auth: Auth!, $supervisor: SupervisorMail!) { | ||
supervisor_password_reset(auth: $auth, supervisor: $supervisor){mail, password} }`, | ||
{auth, supervisor})() | ||
//console.log(result) | ||
const password = result.supervisor_password_reset.password | ||
addPasswort(supervisor, password) | ||
} | ||
|
||
export function SupervisorPasswordReset({supervisor}: {supervisor: string}) { | ||
const {t} = useTranslation() | ||
const auth = useAuthStore() | ||
const {passwortMap, addPasswort} = usePasswortMapStore() | ||
|
||
return ( | ||
<> | ||
<input type="button" onClick={async () => { await pwReset(auth, supervisor, addPasswort) } } | ||
value={ t('Reset Password') as string } /*name={supervisor}*/ /> | ||
| ||
{ passwortMap[supervisor] } | ||
</> | ||
) | ||
} |
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