From: hylom Date: Tue, 30 Oct 2018 11:41:51 +0000 (+0900) Subject: implement /newuser/password API X-Git-Tag: v0.1.6~95 X-Git-Url: http://git.osdn.net/view?p=newslash%2Fnewslash.git;a=commitdiff_plain;h=36df870d9410ef4a40787b19a09784255337d9ed implement /newuser/password API --- diff --git a/src/newslash_web/lib/Newslash/Web.pm b/src/newslash_web/lib/Newslash/Web.pm index b90fff2a..9030eeec 100644 --- a/src/newslash_web/lib/Newslash/Web.pm +++ b/src/newslash_web/lib/Newslash/Web.pm @@ -351,6 +351,7 @@ sub startup { $api->post('/newuser/validate')->to('API::User#validate_new_user'); $api->post('/newuser/create')->to('API::User#create_new_user'); + $api->post('/newuser/password')->to('API::User#update_password'); $api->get('/sidebar/item')->to('API::SidebarItem#get', seclev => 1); diff --git a/src/newslash_web/lib/Newslash/Web/Controller/API/User.pm b/src/newslash_web/lib/Newslash/Web/Controller/API/User.pm index a09a5b9a..01a49f58 100644 --- a/src/newslash_web/lib/Newslash/Web/Controller/API/User.pm +++ b/src/newslash_web/lib/Newslash/Web/Controller/API/User.pm @@ -220,6 +220,27 @@ sub post { return; } +sub update_password { + my $c = shift; + my $params = $c->req->json; + + if ($params->{token}) { + # token mode + if(!$c->users->update_password_by_token($params->{nickname}, + $params->{token}, + $params->{password})) { + $c->render(json => { error => $c->users->last_error || 1 }); + $c->rendered(400); + return; + } + $c->render(json => {}); + return; + } + $c->render(json => { error => 1 }); + $c->rendered(400); + return; +} + sub validate_new_user { my $c = shift; my $params = $c->req->json; @@ -229,14 +250,11 @@ sub validate_new_user { my $nick_regex = qr/^[a-zA-Z_][ a-zA-Z0-9\$_.+!*\\'(),-]{0,19}$/; - my ($id_error, $email_error) = $c->users->validate_new_user($nickname, $email); - - if ($id_error || $email_error) { - $id_error ||= ""; - $email_error ||= ""; + if (!$c->users->validate_new_user($nickname, $email)) { + my $error = $c->users->last_error; $c->render(json => { error => 1, - id_error => $id_error, - email_error => $email_error, + id_error => $error->{id_error} || "", + email_error => $error->{email_error} || "", nickname => $nickname, email => $email }); $c->rendered(400); @@ -265,12 +283,12 @@ sub create_new_user { $email =~ s/\s//g; # strip whitespace - my ($uid, $error) = $c->users->create_new_user($nickname, $email, $options); - - if ($error) { + my $uid = $c->users->create_new_user($nickname, $email, $options); + if(!$uid) { + my $error = $c->users->last_error; $c->render(json => { error => 1, - id_error => $error->{id_error} || 0, - email_error => $error->{email_error} || 0, + id_error => $error->{id_error} || "", + email_error => $error->{email_error} || "", nickname => $nickname, email => $email }); $c->rendered(400); diff --git a/src/newslash_web/lib/Newslash/Web/Controller/Login.pm b/src/newslash_web/lib/Newslash/Web/Controller/Login.pm index 1769c965..fac027c1 100644 --- a/src/newslash_web/lib/Newslash/Web/Controller/Login.pm +++ b/src/newslash_web/lib/Newslash/Web/Controller/Login.pm @@ -60,6 +60,7 @@ sub activation { my $cancel = $c->param("cancel"); my $the_user = $users->select(nickname => $nickname); + my $prefs = { nickname => $nickname, token => $token }; # check if token is correct if (!$the_user || !$token) { @@ -76,19 +77,15 @@ sub activation { } # check if token is valid - my $rs = $users->activation($nickname, $token); + my $rs = $c->users->activation($nickname, $token); if (!$rs) { - $c->res->code(500); - return; - } - if ($rs->{error}) { - $c->render(activation_succeed => 0, error => $rs->{error}); + $c->render(activation_succeed => 0, error => $c->users->last_error); $c->res->code(400); return; } # ok - $c->render(nickname => $nickname, activation_succeed => 1, error => 0); + $c->render(prefs => $prefs, activation_succeed => 1, error => ""); return; } diff --git a/src/newslash_web/public/js/newslash.js b/src/newslash_web/public/js/newslash.js index 3f69a843..7df714a8 100644 --- a/src/newslash_web/public/js/newslash.js +++ b/src/newslash_web/public/js/newslash.js @@ -122,6 +122,18 @@ function _initNewslash() { return this.post(url, data); }; + /* user API */ + Newslash.prototype.updatePasswordByToken = function (username, token, password, options) { + options = options || {}; + var url = "/newuser/password"; + var data = { nickname: username, + token: token, + password: password, + options: options, + }; + return this.post(url, data); + }; + /* comments API */ Newslash.prototype.getComments = function getComments (discussionID, parentID) { if (!discussionID) return this.fail(); diff --git a/src/newslash_web/public/js/update-password.js b/src/newslash_web/public/js/update-password.js new file mode 100644 index 00000000..6c73e458 --- /dev/null +++ b/src/newslash_web/public/js/update-password.js @@ -0,0 +1,57 @@ +var updatePassword = {}; + +updatePassword.run = function run(params) { + + Vue.component('message', { + props: ["target", ], + template: '#message', + }); + + var data = { + password: "", + password2: "", + token: params.token || "", + nickname: params.nickname || "", + passwordError: "BLANK_PASSWORD", + formError: true, + state: "", + }; + + var methods = {}; + methods.doPost = function doPost() { + this.state = "POSTING"; + newslash.updatePasswordByToken(this.nickname, this.token, this.password).then( + (resp) => { + // ok + this.state = "SUCCEED"; + }, + (resp) => { + // failed + this.passwordError = resp.error || false; + this.formError = true; + this.state = ""; + }); + }; + + // form value validations + var watch = {}; + watch.password = function watchPassword(val, oldVal) { + if (val.length == 0) { this.passwordError = "BLANK_PASSWORD"; } + else if (val != this.password2) { this.passwordError = "PASSWORD_NOT_MATCH"; } + else { this.passwordError = ""; } + this.formError = this.passwordError; + }; + watch.password2 = function watchPassword2(val, oldVal) { + if (val != this.password) { this.passwordError = "PASSWORD_NOT_MATCH"; } + else { this.passwordError = ""; } + this.formError = this.passwordError; + }; + + var vm = new Vue({ + el: params.el, + data: data, + methods: methods, + watch: watch, + }); + +}; diff --git a/src/newslash_web/templates/login/activation.html.tt2 b/src/newslash_web/templates/login/activation.html.tt2 index 32283233..2e78617e 100644 --- a/src/newslash_web/templates/login/activation.html.tt2 +++ b/src/newslash_web/templates/login/activation.html.tt2 @@ -1,26 +1,31 @@ [% WRAPPER common/layout %] -[%- IF activation_succeed -%] - -
+[%- IF activation_succeed -%] +

パスワード設定

+ +
- ユーザー「[% nickname %]」で使用するパスワードを入力してください。 + ユーザー「[% prefs.nickname %]」で使用するパスワードを入力してください。
-
+
+ パスワードを設定しました。ログインページからこのパスワードでログインできます。 +
[% helpers.load_js("activation.js") %] [%- ELSE #activation_succeed -%] -
+

認証エラー

[%- IF error == "TOKEN_EXPIRED" %]