Skip to content

Commit a2a47e3

Browse files
committed
Completed:log in, todo: access current user
1 parent 428274b commit a2a47e3

File tree

8 files changed

+95
-8
lines changed

8 files changed

+95
-8
lines changed

Application/Schema.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,11 @@ CREATE TABLE comments (
1616
CREATE INDEX comments_post_id_index ON comments (post_id);
1717
CREATE INDEX comments_created_at_index ON comments (created_at);
1818
ALTER TABLE comments ADD CONSTRAINT comments_ref_post_id FOREIGN KEY (post_id) REFERENCES posts (id) ON DELETE NO ACTION;
19+
20+
CREATE TABLE users (
21+
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
22+
email TEXT NOT NULL,
23+
password_hash TEXT NOT NULL,
24+
locked_at TIMESTAMP WITH TIME ZONE DEFAULT NULL,
25+
failed_login_attempts INT DEFAULT 0 NOT NULL
26+
);

Web/Controller/Sessions.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Web.Controller.Sessions where
2+
3+
import Web.Controller.Prelude
4+
import Web.View.Sessions.New
5+
import qualified IHP.AuthSupport.Controller.Sessions as Sessions
6+
7+
instance Controller SessionsController where
8+
action NewSessionAction = Sessions.newSessionAction @User
9+
action CreateSessionAction = Sessions.createSessionAction @User
10+
action DeleteSessionAction = Sessions.deleteSessionAction @User
11+
12+
instance Sessions.SessionsControllerConfig User

Web/FrontController.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import IHP.RouterPrelude
44
import Web.Controller.Prelude
55
import Web.View.Layout (defaultLayout)
66

7+
import IHP.LoginSupport.Middleware
8+
import Web.Controller.Sessions
9+
710
-- Controller Imports
811
import Web.Controller.Comments
912
import Web.Controller.Posts
@@ -15,9 +18,14 @@ instance FrontController WebApplication where
1518
-- Generator Marker
1619
, parseRoute @CommentsController
1720
, parseRoute @PostsController
21+
, parseRoute @SessionsController
1822
]
1923

2024
instance InitControllerContext WebApplication where
2125
initContext = do
2226
setLayout defaultLayout
2327
initAutoRefresh
28+
initAuthentication @User
29+
30+
31+

Web/Routes.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ instance AutoRoute PostsController
99

1010

1111
instance AutoRoute CommentsController
12+
instance AutoRoute SessionsController
13+
1214

Web/Types.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module Web.Types where
33
import IHP.Prelude
44
import IHP.ModelSupport
55
import Generated.Types
6+
import IHP.LoginSupport.Types -- <---- ADD THIS IMPORT
7+
68

79
data WebApplication = WebApplication deriving (Eq, Show)
810

@@ -28,3 +30,15 @@ data CommentsController
2830
| UpdateCommentAction { commentId :: !(Id Comment) }
2931
| DeleteCommentAction { commentId :: !(Id Comment) }
3032
deriving (Eq, Show, Data)
33+
34+
35+
instance HasNewSessionUrl User where
36+
newSessionUrl _ = "/NewSession"
37+
38+
type instance CurrentUserRecord = User
39+
40+
data SessionsController
41+
= NewSessionAction
42+
| CreateSessionAction
43+
| DeleteSessionAction
44+
deriving (Eq, Show, Data)

Web/View/Comments/Show.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ import Web.View.Prelude
44
data ShowView = ShowView { comment :: Comment }
55

66
instance View ShowView where
7-
html ShowView { .. } = [hsx|
7+
html ShowView { comment } = [hsx|
88
{breadcrumb}
99
<h1>Show Comment</h1>
1010
<p>{comment}</p>
1111

12+
<form method="POST" action={DeleteCommentAction (get #id comment)}>
13+
<input type="hidden" name="_method" value="DELETE"/>
14+
<button type="submit">Delete Comment</button>
15+
</form>
1216
|]
1317
where
1418
breadcrumb = renderBreadcrumb

Web/View/Sessions/New.hs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module Web.View.Sessions.New where
2+
import Web.View.Prelude
3+
import IHP.AuthSupport.View.Sessions.New
4+
5+
instance View (NewView User) where
6+
html NewView { .. } = [hsx|
7+
<div class="h-100" id="sessions-new">
8+
<div class="d-flex align-items-center">
9+
<div class="w-100">
10+
<div style="max-width: 400px" class="mx-auto mb-5">
11+
<h5>Please login</h5>
12+
{renderForm user}
13+
</div>
14+
</div>
15+
</div>
16+
</div>
17+
|]
18+
19+
renderForm :: User -> Html
20+
renderForm user = [hsx|
21+
<form method="POST" action={CreateSessionAction}>
22+
<div class="form-group">
23+
<input name="email" value={user.email} type="email" class="form-control" placeholder="E-Mail" required="required" autofocus="autofocus" />
24+
</div>
25+
<div class="form-group">
26+
<input name="password" type="password" class="form-control" placeholder="Password"/>
27+
</div>
28+
<button type="submit" class="btn btn-primary btn-block">Login</button>
29+
</form>
30+
|]

Web/View/Static/Welcome.hs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,33 @@ instance View WelcomeView where
1212
</h1>
1313

1414
<h2 style="margin-top: 0; margin-bottom: 0rem; font-weight: 900; font-size: 3rem">
15-
Hello World!
15+
Reddit Clone
1616
</h2>
1717

18-
<p style="margin-top: 1rem; font-size: 1.75rem; font-weight: 600; color:hsla(196, 13%, 80%, 1)">
19-
Your new application is up and running.
20-
</p>
18+
<!-- <p style="margin-top: 1rem; font-size: 1.75rem; font-weight: 600; color:hsla(196, 13%, 80%, 1)">
19+
<a href={NewSessionAction}>Login</a>
20+
</p> -->
2121

2222
<p>
23+
<a href={NewSessionAction}
24+
style="margin-top: 2rem; background-color: #268bd2; padding: 1rem; border-radius: 3px; color: hsla(205, 69%, 98%, 1); text-decoration: none; font-weight: bold; display: inline-block; box-shadow: 0 4px 6px hsla(205, 69%, 0%, 0.08); transition: box-shadow 0.2s; transition: transform 0.2s;"
25+
target="_blank"
26+
>Log in
27+
</a>
28+
29+
</p>
30+
31+
<!-- <p>
2332
<a
2433
href="https://ihp.digitallyinduced.com/Slack"
2534
style="margin-top: 2rem; background-color: #268bd2; padding: 1rem; border-radius: 3px; color: hsla(205, 69%, 98%, 1); text-decoration: none; font-weight: bold; display: inline-block; box-shadow: 0 4px 6px hsla(205, 69%, 0%, 0.08); transition: box-shadow 0.2s; transition: transform 0.2s;"
2635
target="_blank"
2736
>Join our community on Slack!</a>
28-
</p>
37+
</p> -->
2938

30-
<a href="https://ihp.digitallyinduced.com/Guide/your-first-project.html" style="margin-top: 2rem; background-color: #268bd2; padding: 1rem; border-radius: 3px; color: hsla(205, 69%, 98%, 1); text-decoration: none; font-weight: bold; display: inline-block; box-shadow: 0 4px 6px hsla(205, 69%, 0%, 0.08); transition: box-shadow 0.2s; transition: transform 0.2s;" target="_blank">
39+
<!-- <a href="https://ihp.digitallyinduced.com/Guide/your-first-project.html" style="margin-top: 2rem; background-color: #268bd2; padding: 1rem; border-radius: 3px; color: hsla(205, 69%, 98%, 1); text-decoration: none; font-weight: bold; display: inline-block; box-shadow: 0 4px 6px hsla(205, 69%, 0%, 0.08); transition: box-shadow 0.2s; transition: transform 0.2s;" target="_blank">
3140
Learn the Next Steps in the Documentation
32-
</a>
41+
</a> -->
3342
</div>
3443
</div>
3544

0 commit comments

Comments
 (0)