Skip to content

Commit

Permalink
Facebook auth fixed, deleting user fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
jernejle committed Apr 24, 2012
1 parent fc2ac91 commit 6fe829f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 105 deletions.
5 changes: 1 addition & 4 deletions CodeBase/CodeBase.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,6 @@
<ItemGroup>
<Content Include="Views\Articles\RenderFiles.cshtml" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\Account\ChangeDisplayName.cshtml" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\Articles\EditorMode.cshtml" />
</ItemGroup>
Expand Down Expand Up @@ -530,4 +527,4 @@
</VisualStudio>
</ProjectExtensions>
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
</Project>
</Project>
48 changes: 1 addition & 47 deletions CodeBase/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public ActionResult LogOn(LogOnModel model, string returnUrl)

public ActionResult LogOff()
{
Session["accessToken"] = null;
FormsAuthentication.SignOut();

return RedirectToAction("Index", "Home");
Expand Down Expand Up @@ -158,53 +159,6 @@ public ActionResult ChangePasswordSuccess()
return View();
}

public ActionResult ChangeDisplayName()
{
if (Session["accessToken"] != null && Session["fbuserchosenname"] == null && Session["fbid"] != null)
{
return View();
}
return RedirectToAction("Index", "Home");
}

[HttpPost]
public ActionResult ChangeDisplayName(String username)
{

if (Session["accessToken"] != null && Session["fbuserchosenname"] == null && Session["fbid"] != null)
{
User u = context.Users.SingleOrDefault(x => x.Username == username);
if (u != null)
{
ViewData["err"] = "Display name already exists";
return View();
}

MembershipCreateStatus createStatus;
Membership.CreateUser(username, Session["accessToken"] as String, null, null, null, true, null, out createStatus);

if (createStatus == MembershipCreateStatus.Success)
{
User newfbuser = new User()
{
Username = username,
FbId = Convert.ToInt32(Session["fbid"]),
JoinDate = DateTime.Now,
};

context.Users.Add(newfbuser);
context.SaveChanges();
Roles.AddUserToRole(newfbuser.Username, "Normal");
FormsAuthentication.SetAuthCookie(newfbuser.Username, true);
Session["fbid"] = null;
Session["fbuserchosenname"] = newfbuser.Username;
}

return RedirectToAction("Index", "Home");
}
return RedirectToAction("Index", "Home");
}

#region Status Codes
private static string ErrorCodeToString(MembershipCreateStatus createStatus)
{
Expand Down
46 changes: 32 additions & 14 deletions CodeBase/Controllers/FacebookController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
Expand All @@ -19,51 +20,68 @@ public class FacebookController : Controller
[HttpPost]
public String Login(FormCollection form)
{

dynamic fbuser;
if (Session["accessToken"] == null)
{
Session["accessToken"] = form["accessToken"];
FacebookClient c = new FacebookClient(form["accessToken"].ToString());
User u = null;
int fbuserid = -1;

fbuser = c.Get("me");

try
{
fbuserid = Convert.ToInt32(fbuser.id);
}
catch (Exception e)
{}
{
Console.WriteLine(e.ToString());
}

u = context.Users.SingleOrDefault(x => x.FbId == fbuserid);

if (u == null)
if (u == null && fbuserid != -1)
{
Session["fbid"] = fbuserid;
return "null";
//generate nick and add user to db&membership
String name = fbuser.name + fbuserid;

MembershipCreateStatus createStatus;
Membership.CreateUser(name, Session["accessToken"] as String, null, null, null, true, null, out createStatus);

if (createStatus == MembershipCreateStatus.Success)
{
User newfbuser = new User()
{
Username = name,
FbId = fbuserid,
JoinDate = DateTime.Now,
};

context.Users.Add(newfbuser);
context.SaveChanges();
Roles.AddUserToRole(newfbuser.Username, "Normal");
FormsAuthentication.SetAuthCookie(newfbuser.Username, true);
return newfbuser.Username;
}
else
{
return "error";
}
}
else if (u != null && fbuserid != -1)
{
//login user with fb account
FormsAuthentication.SetAuthCookie(u.Username, true);
Session["fbuserchosenname"] = u.Username;
return u.Username;
}
}
String localname = Session["fbuserchosenname"] as String;
if (localname != null)
{
return localname;
}
return "";
}

[HttpPost]
public void Logout(FormCollection form)
{
Session["accessToken"] = null;
Session["fbid"] = null;
Session["fbuserchosenname"] = null;
FormsAuthentication.SignOut();
}

Expand Down
1 change: 1 addition & 0 deletions CodeBase/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public ActionResult Delete(int id)
public ActionResult DeleteConfirmed(int id)
{
User user = context.Users.Single(x => x.UserId == id);
Membership.DeleteUser(user.Username);
context.Users.Remove(user);
context.SaveChanges();
return RedirectToAction("Index");
Expand Down
30 changes: 0 additions & 30 deletions CodeBase/Views/Account/ChangeDisplayName.cshtml

This file was deleted.

17 changes: 7 additions & 10 deletions CodeBase/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,10 @@
<li>@Html.ActionLink("Q&A","Index","Questions")</li>
@if (Request.IsAuthenticated)
{
<li class="user-name">@User.Identity.Name</li>
}
else if (Session["accessToken"] != null && Session["fbid"] != null)
{
<li class="login">@Html.ActionLink("Choose your name", "ChangeDisplayName", "Account")</li>
} else
{
<li id="uname" class="user-name">@User.Identity.Name</li>
}
else
{
<li class="login">@Html.ActionLink("Login", "LogOn", "Account")</li>
}
</ul>
Expand Down Expand Up @@ -192,9 +189,9 @@
type: "POST",
data: { "accessToken": response.authResponse.accessToken },
success: function (data) {
if (data == "null") {
window.location = "/Account/ChangeDisplayName";
}
if (data == "error") {
alert("Something went wrong");
}
}
});
Expand Down

0 comments on commit 6fe829f

Please sign in to comment.