Permalink
Browse files

Added the verify email page and tidied the verification function

  • Loading branch information...
1 parent 2a0886b commit b7cba96bde68397d04c1bee613a836cfbee0f1f1 @Joony committed Jul 28, 2009
Binary file not shown.
Binary file not shown.
View
Binary file not shown.
Binary file not shown.
View
Binary file not shown.
View
@@ -20,7 +20,7 @@
-module(db_users).
-include("wf.inc").
-include("config.inc").
--export([init/0, add_user/3, validate_user/2, delete_user/1, is_username_used/1, is_email_used/1, get_email_address/1, verify_email/1, update_verification_code/2, invalidate_email/1, delete_email_verification_code/1, new_email_verification_code/1]).
+-export([init/0, add_user/3, validate_user/2, delete_user/1, is_username_used/1, is_email_used/1, get_email_address/1, verify_email/1, invalidate_email/1, delete_email_verification_code/1, new_email_verification_code/1]).
-include_lib("stdlib/include/qlc.hrl").
@@ -101,18 +101,6 @@ delete_user(Username) ->
end,
mnesia:transaction(F).
-%% not implemented yet, but will be used for the forgot password process if the user has already requested a code.
-update_verification_code(EmailAddress) ->
- update_verification_code(EmailAddress, "").
-
-update_verification_code(EmailAddress, Code) ->
- F = fun() ->
- [E] = mnesia:read(verification_codes_email, Code, write),
- Update = E#verification_codes_email{verification_code=Code},
- mnesia:write(Update)
- end,
- mnesia:transaction(F).
-
%% result seems backwards compared to the name
is_username_used(Username) ->
case db_utils:do(qlc:q ([X#users.username || X <- mnesia:table(users), string:equal(X#users.username, Username)])) of
@@ -158,22 +146,27 @@ verify_email(Code) ->
FGetUsername = fun() ->
mnesia:match_object({verification_codes_email, '_', Code})
end,
- {atomic, [VerificationCodesEmailRow]} = mnesia:transaction(FGetUsername),
- Username = VerificationCodesEmailRow#verification_codes_email.username,
- io:format("Username is: ~s~n", [Username]),
- FUpdateVerifiedEmail = fun() ->
- [VerificationLevels] = mnesia:read(verification_levels, Username, write),
- VerificationLevelsUpdate = VerificationLevels#verification_levels{verified_email=true},
- mnesia:write(VerificationLevelsUpdate)
- end,
- io:format("Test: ~w~n", [mnesia:transaction(FUpdateVerifiedEmail)]),
- delete_email_verification_code(Username).
+ case mnesia:transaction(FGetUsername) of
+ {atomic, [VerificationCodesEmailRow]} ->
+ %{atomic, [VerificationCodesEmailRow]} = mnesia:transaction(FGetUsername),
+ Username = VerificationCodesEmailRow#verification_codes_email.username,
+ FUpdateVerifiedEmail = fun() ->
+ [VerificationLevels] = mnesia:read(verification_levels, Username, write),
+ VerificationLevelsUpdate = VerificationLevels#verification_levels{verified_email=true},
+ mnesia:write(VerificationLevelsUpdate)
+ end,
+ mnesia:transaction(FUpdateVerifiedEmail),
+ delete_email_verification_code(Username);
+ _ ->
+ %io:format("Invalid verification code~n"),
+ {aborted, "invalid verification code"}
+ end.
delete_email_verification_code(Username) ->
F = fun() ->
mnesia:delete({verification_codes_email, Username})
end,
- io:format("Delete email verification code: ~w~n", [mnesia:transaction(F)]).
+ mnesia:transaction(F).
invalidate_email(Username) ->
FUpdateVerifiedEmail = fun() ->
@@ -45,7 +45,7 @@ event(login) ->
case db_users:validate_user(hd(wf:q(username)), hd(wf:q(password))) of
{ valid, _ID } ->
io:format("User: ~s has logged in~n", [wf:q(username)]),
- wf:flash("Correct"),
+ %wf:flash("Correct"),
wf:user(hd(wf:q(username))),
wf:redirect("dashboard");
_ ->
@@ -0,0 +1,25 @@
+-module (web_users_verify_email).
+-include_lib ("nitrogen/include/wf.inc").
+-compile(export_all).
+
+main() ->
+ [Code] = wf:q("key"),
+ case db_users:verify_email(Code) of
+ {atomic,ok} ->
+ io:format("Everything looks ok~n");
+ {aborted, "invalid verification code"} ->
+ io:format("Invalid verification code~n");
+ _ ->
+ io:format("Something went wrong~n")
+ end,
+
+#template { file="./wwwroot/template.html"}.
+
+
+title() ->
+ "web_users_verify_email".
+
+body() ->
+ #label{text="web_users_verify_email body."}.
+
+event(_) -> ok.

0 comments on commit b7cba96

Please sign in to comment.