質問編集履歴

1

追加依頼

2021/02/14 00:34

投稿

hokosugi
hokosugi

スコア63

test CHANGED
File without changes
test CHANGED
@@ -43,3 +43,111 @@
43
43
 
44
44
 
45
45
  宜しくおねがいします。
46
+
47
+ 【修正後】
48
+
49
+ 上記のコードの全文(controller.js)と`view/login.ejs`を追加します。
50
+
51
+
52
+
53
+ ```
54
+
55
+ const mongoose = require('mongoose');
56
+
57
+ const { body, validationResult } = require('express-validator');
58
+
59
+ const schema = require('../models/model');
60
+
61
+ const Model = mongoose.model('Model', schema);
62
+
63
+
64
+
65
+ exports.login = function(req, res){
66
+
67
+ res.render('login', {title: 'Laravel'});
68
+
69
+ };
70
+
71
+ exports.loginPost =
72
+
73
+ function(req, res){
74
+
75
+ const errors = validationResult(req);
76
+
77
+ const model = new Model();
78
+
79
+ console.log(body);
80
+
81
+ if (!errors.isEmpty()) {
82
+
83
+ return res.status(400).json({ errors: errors.array() });
84
+
85
+ };
86
+
87
+ body('passwordConfirmation').custom((value, req) => {
88
+
89
+ //console.log(req.body.password);
90
+
91
+ console.log(body.passwordConfirmation);
92
+
93
+ if (value !== req.body.password) {
94
+
95
+ throw new Error('パスワード確認がパスワードと一致しません');
96
+
97
+ }
98
+
99
+ return true;
100
+
101
+ }),
102
+
103
+ model.save(function(err){
104
+
105
+ if (err) {
106
+
107
+ res.status(403).send('error:'+err);
108
+
109
+ } else {
110
+
111
+ res.render('home', {title: 'Laravel', user: req.body.name});
112
+
113
+ }
114
+
115
+ });
116
+
117
+ }
118
+
119
+ ```
120
+
121
+ ```login.ejs
122
+
123
+ <%- include('partials/header') %>
124
+
125
+ <div id="big-container">
126
+
127
+ <div id="login-first-container"><%= title %></div>
128
+
129
+ <div id="login-second-container">
130
+
131
+ <form action="#" method="post">
132
+
133
+ <p>name<input type="text" name="name"></p>
134
+
135
+ <p>Password<input type="text" name="password"></p>
136
+
137
+ <p>Password(確認)<input type="text" name="passwordConfirmation"></p>
138
+
139
+ <button name="login">Login</button>
140
+
141
+ <a href="#">Forget Your Password?</a>
142
+
143
+ </form>
144
+
145
+
146
+
147
+ </div>
148
+
149
+ </div>
150
+
151
+ <%-include('partials/footer') %>
152
+
153
+ ```