forked from glotcode/glot-www
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccount.hs
55 lines (48 loc) · 1.67 KB
/
Account.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
{-# LANGUAGE DeriveGeneric #-}
module Handler.Account where
import Import
import Yesod.Auth.Simple (setPasswordR)
import Util.Slug (mkSlug)
import Util.User (newToken)
import Util.Handler (title)
import Util.Alert (successHtml)
data ProfileData = ProfileData {
name :: Text,
username :: Text
} deriving (Show, Generic)
instance FromJSON ProfileData
getAccountProfileR :: Handler Html
getAccountProfileR = do
userId <- requireAuthId
Entity _ profile <- runDB $ getBy404 $ UniqueProfile userId
defaultLayout $ do
setTitle $ title "Profile"
$(widgetFile "account/profile")
putAccountProfileR :: Handler Value
putAccountProfileR = do
userId <- requireAuthId
profileData <- requireCheckJsonBody :: Handler ProfileData
Entity profileId _ <- runDB $ getBy404 $ UniqueProfile userId
now <- liftIO getCurrentTime
runDB $ update profileId [
ProfileName =. name profileData,
ProfileUsername =. (mkSlug $ username profileData),
ProfileModified =. now]
setMessage $ successHtml "Profile updated"
return $ object []
getAccountTokenR :: Handler Html
getAccountTokenR = do
userId <- requireAuthId
Entity _ apiUser <- runDB $ getBy404 $ UniqueApiUser userId
defaultLayout $ do
setTitle $ title "Api token"
$(widgetFile "account/token")
putAccountTokenR :: Handler Value
putAccountTokenR = do
userId <- requireAuthId
Entity apiUserId _ <- runDB $ getBy404 $ UniqueApiUser userId
token <- liftIO newToken
now <- liftIO getCurrentTime
runDB $ update apiUserId [ApiUserToken =. token, ApiUserModified =. now]
setMessage $ successHtml "New token generated"
return $ object []