Demo script for sign in by wordpress


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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
require_once dirname(__FILE__).'/wp-load.php';

$action = $_GET['action'];

if ($action === 'login') {
$request = file_get_contents('php://input');
$data = json_decode($request);

$username = $data->username;
$password = $data->password;

$user = wp_signon([
'user_login' => $username,
'user_password' => $password,
'remember' => false,
], false);

$ok = false;
$message = '';
$data = '';

if (is_wp_error($user) === true) {
$ok = false;
$message = 'The user information incorrect. Cannot signed in.';
$data = null;
}else{
$ok = true;
$message = 'Successfully signed in.';
$data = $user;
}

echo json_encode([
'ok' => $ok,
'message' => $message,
'data' => $user
]);

exit;
}

if ($action === 'profile') {
$status = is_user_logged_in();

echo json_encode([
'ok' => $status,
'message' => $status === false ? 'Please sign in first' : 'Successfully signed in',
'data' => wp_get_current_user()
]);

exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>A_A</title>
<style type="text/css">
body {
margin-top: 60px;
}

.full-width {
width: 100%;
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<div class="sign-in-page" v-if="is_logged_in === false">
<h3>Sign in</h3>
<hr>
<div class="alert alert-danger" role="alert" v-if="alert.enabled"></div>
<div class="form">
<div class="form-group row">
<label for="username" class="col-lg-2 col-form-label">Username</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="username" v-model="username">
</div>
</div>
<div class="form-group row">
<label for="password" class="col-lg-2 col-form-label">Password</label>
<div class="col-lg-10">
<input type="password" class="form-control" id="password" v-model="password">
</div>
</div>
<button type="button" class="btn btn-primary full-width" v-on:click="login">Login</button>
</div>
</div>

<div class="signed-in-page" v-if="is_logged_in === true">
<h3>User information</h3>
<hr>
<div class="card mt-1">
<div class="card-body p-0">
<div class="container">
<div class="row">
<div class="col-lg-2 pt-2 pb-2 text-center bg-info text-light font-weight-bold">ID</div>
<div class="col-lg-10 pt-2 pb-2"></div>
</div>
</div>
</div>
</div>
<div class="card mt-1">
<div class="card-body p-0">
<div class="container">
<div class="row">
<div class="col-lg-2 pt-2 pb-2 text-center bg-info text-light font-weight-bold">Display name</div>
<div class="col-lg-10 pt-2 pb-2"></div>
</div>
</div>
</div>
</div>
<div class="card mt-1">
<div class="card-body p-0">
<div class="container">
<div class="row">
<div class="col-lg-2 pt-2 pb-2 text-center bg-info text-light font-weight-bold">Email</div>
<div class="col-lg-10 pt-2 pb-2"></div>
</div>
</div>
</div>
</div>
<div class="card mt-1">
<div class="card-body p-0">
<div class="container">
<div class="row">
<div class="col-lg-2 pt-2 pb-2 text-center bg-info text-light font-weight-bold">Nicename</div>
<div class="col-lg-10 pt-2 pb-2"></div>
</div>
</div>
</div>
</div>
<div class="card mt-1">
<div class="card-body p-0">
<div class="container">
<div class="row">
<div class="col-lg-2 pt-2 pb-2 text-center bg-info text-light font-weight-bold">Register date</div>
<div class="col-lg-10 pt-2 pb-2"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
new Vue({
el: "#app",

data: function() {
return {
username: "",
password: "",

is_logged_in: false,

alert: {
enabled: false,
message: ""
}
}
},

created: function() {
var self = this;

axios.get(window.location.pathname, {
params: {
action: 'profile'
}
}).then(function(response) {
var data = response.data;

if (data.ok === true) {
var data = data.data;
var user = data.data;

self.is_logged_in = true;
self.user = user;
}
}).catch(function(error) {
alert('Got unknown error when request profile');

console.log(error);
})
},

methods: {
login: function() {
var self = this;
var username = this.username;
var password = this.password;

axios.post('?action=login', {
username: username,
password: password
}).then(function(response) {
var data = response.data;

if (data.ok === false) {
self.alert.enabled = true;
self.alert.message = data.message;
}else{
var data = data.data;
var user = data.data;

self.is_logged_in = true;
self.user = user;
}
}).catch(function(error) {
alert('Got unknown error when request login');

console.log(error);
})
}
}

})
</script>
</body>
</html>