-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuth.hs
112 lines (99 loc) · 3.67 KB
/
Auth.hs
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
{-# LANGUAGE RankNTypes #-}
module Auth ( authEmail
, YesodAuthEmail (..)
, EmailCreds (..)
, saltPass
, isValidPass
, loginR
, registerR
, forgotPasswordR
, setpassR
) where
import Prelude hiding (head, init, last, readFile, tail, writeFile)
import Control.Monad (void)
import Data.Text (Text)
import Yesod.Auth
import Yesod.Auth.Email ( YesodAuthEmail (..)
, EmailCreds (..)
, saltPass
, isValidPass
, loginR
, registerR
, forgotPasswordR
, setpassR
)
import Yesod.Core
import qualified Yesod.Auth.Email as AE
import qualified Yesod.Auth.Message as Msg
import Settings
data AuthMessage = MsgResetButton
| MsgLoginButton
| MsgRegisterButton
| MsgForgotButton
| MsgCurrentPassword
instance YesodAuthEmail master => RenderMessage master AuthMessage where
renderMessage _ ("en":_) = renderEnglish
renderMessage _ ("de":_) = renderGerman
renderMessage _ ("de-DE":_) = renderGerman
renderMessage master (_:langs) = renderMessage master langs
renderMessage _ _ = renderDefault
renderDefault :: AuthMessage -> Text
renderDefault = renderEnglish
renderEnglish :: AuthMessage -> Text
renderEnglish MsgResetButton = "Reset"
renderEnglish MsgLoginButton = "Login"
renderEnglish MsgRegisterButton = "Register"
renderEnglish MsgForgotButton = "Recover forgotten password"
renderEnglish MsgCurrentPassword = "Current password"
renderGerman :: AuthMessage -> Text
renderGerman MsgResetButton = "Zurücksetzen"
renderGerman MsgLoginButton = "Anmelden"
renderGerman MsgRegisterButton = "Registrieren"
renderGerman MsgForgotButton = "Vergessenes Passwort zurücksetzen"
renderGerman MsgCurrentPassword = "Aktuelles Passwort"
origAuth :: YesodAuthEmail master => AuthPlugin master
origAuth = AE.authEmail
authEmail :: YesodAuthEmail master => AuthPlugin master
authEmail = origAuth { apDispatch = dispatch } { apLogin = login }
login :: YesodAuthEmail master => (Route Auth -> Route master) -> WidgetFor master ()
login toMaster = do
email <- newIdent
pwd <- newIdent
$(widgetFile "login")
dispatch :: YesodAuthEmail master => Text -> [Text] -> AuthHandler master TypedContent
dispatch "GET" ["register"] = getRegisterR >>= sendResponse
dispatch "GET" ["forgot-password"] = getForgotPasswordR >>= sendResponse
dispatch "GET" ["set-password"] = getPasswordR >>= sendResponse
dispatch method params = origDispatch method params
where origDispatch = apDispatch origAuth
getRegisterR :: YesodAuthEmail master => AuthHandler master Html
getRegisterR = do
email <- newIdent
toParent <- getRouteToParent
request <- getRequest
liftHandler . defaultLayout $ do
setTitleI Msg.RegisterLong
$(widgetFile "register")
getForgotPasswordR :: YesodAuthEmail master => AuthHandler master Html
getForgotPasswordR = do
email <- newIdent
toParent <- getRouteToParent
request <- getRequest
liftHandler . defaultLayout $ do
setTitleI Msg.PasswordResetTitle
$(widgetFile "forgot-password")
getPasswordR :: YesodAuthEmail master => AuthHandler master Html
getPasswordR = do
mAuthId <- maybeAuthId
case mAuthId of
Just _ -> return ()
Nothing -> void $ loginErrorMessageI LoginR Msg.BadSetPass
pwdCur <- newIdent
pwdNew <- newIdent
pwdCon <- newIdent
toParent <- getRouteToParent
needOld <- maybe (return True) needOldPassword mAuthId
request <- getRequest
liftHandler . defaultLayout $ do
setTitleI Msg.SetPassTitle
$(widgetFile "password")