Skip to content

Instantly share code, notes, and snippets.

@2947721120
Last active July 23, 2016 10:43
Show Gist options
  • Save 2947721120/35020fe306f16b1959251424d943e285 to your computer and use it in GitHub Desktop.
Save 2947721120/35020fe306f16b1959251424d943e285 to your computer and use it in GitHub Desktop.
Angular + Firebase + OAuth

Angular + Firebase + OAuth 角+火力数据+第三方登陆(谷歌,github,脸谱等)

A Pen by 杨俊 on CodePen.

License.

<html ng-app="fireLearning">
<head>
<meta charset="UTF-8">
<title>消防学习</title>
<!-- Firebase -->
<script src="https://firebase.c2cbc.com/js/client/2.2.7/firebase.js"></script>
<!-- Angular -->
<script src="https://ajax.c2cmalls.com/ajax/libs/angularjs/1.4.0/angular.js"></script>
<script src="https://ajax.c2cmalls.com/ajax/libs/angularjs/1.4.0/angular-route.js"></script>
<!-- AngularFire -->
<script src="https://firebase.c2cbc.com/libs/angularfire/1.1.1/angularfire.min.js"></script>
<script src="js/app.js"></script>
<link rel="stylesheet" href="css/app.css">
<link rel="stylesheet" href="https://cdn.c2cmalls.com/bootstrap/3.3.4/css/bootstrap.min.css">
</head>
<body ng-controller="AuthCtrl">
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header" ng-show="authData">
<a class="navbar-brand" href="">
<img class="profileImage" ng-src="{{getUserImage()}}"/>
</a>
</div>
<p ng-show="authData" class="navbar-text">{{cachedProfile.name}} - {{cachedProfile.email}}</p>
<ul class="nav navbar-nav navbar-right">
<li ng-click="login('google')" ng-hide="authData"><a href>登录谷歌</a></li>
<li ng-click="login('github')" ng-hide="authData"><a href>Github</a></li>
<li ng-click="login('facebook')" ng-hide="authData"><a href>Facebook</a></li>
<li ng-click="logout()" ng-show="authData"><a href>退出</a></li>
</ul>
</div>
</nav>
<div class="content" ng-show="authData">
</div>
</body>
var app = angular.module("fireLearning", ["firebase","ngRoute"]);
var ref = new Firebase("https://glowing-inferno-7011.firebaseio.com");
app.config(['$routeProvider', '$locationProvider',function($routeProvider, $locationProvider){
$routeProvider
.when('/',{
controller: "AuthCtrl",
templateUrl: "/index.html"
}).
otherwise({
redirectTo: '/'
});
}]);
var app = angular.module("fireLearning", ["firebase"]);
app.factory("Auth", function($firebaseAuth){
return $firebaseAuth(ref);
});
app.controller("AuthCtrl", function($scope, Auth, $location){
$scope.provider = '';
$scope.authData;
Auth.$onAuth(function(authData){
$scope.authData = authData;
if(authData) {
$scope.cachedProfile = getCachedProfile();
createUser();
//$location.path("/authenticated");
}
console.log($scope.authData);
});
$scope.login = function(provider) {
Auth.$authWithOAuthPopup(provider, { scope: 'email' })
.catch(function(error){
console.error(error);
})
}
$scope.logout = function() {
Auth.$unauth();
}
var createUser = function() {
ref.createUser($scope.cachedProfile, function(error, userData) {
if (error) {
switch (error.code) {
case "EMAIL_TAKEN":
console.log("无法创建新的用户帐户,因为电子邮件已经在使用中了.");
break;
case "INVALID_EMAIL":
console.log("指定的电子邮件不是一个有效的电子邮件.");
break;
default:
console.log("错误创建用户:", error);
}
} else {
console.log("成功创建用户帐户UID:", userData.uid);
}
});
}
var getCachedProfile = function() {
if(!$scope.authData) return "";
switch($scope.authData.provider) {
case "github":
return $scope.authData.github.cachedUserProfile;
break;
case "facebook":
return $scope.authData.facebook.cachedUserProfile;
break;
case "google":
return $scope.authData.google.cachedUserProfile;
break;
default:
return "";
}
}
$scope.getUserImage = function() {
if(!$scope.authData) return "";
switch($scope.authData.provider) {
case "github":
return $scope.authData.github.cachedUserProfile.avatar_url ? $scope.authData.github.cachedUserProfile.avatar_url : "";
break;
case "facebook":
return $scope.authData.facebook.profileImageURL ? $scope.authData.facebook.profileImageURL : "";
break;
case "google":
return $scope.authData.google.profileImageURL ? $scope.authData.google.profileImageURL : "";
break;
default:
return "";
}
}
});
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular.min.js"></script>
body {
padding: 10px;
font-size: 16px !important;
font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
}
/****************/
.heading-lbl {
font-weight: 500;
font-size: 18px;
margin-top: 5px;
}
.sc-float-left {
float: left;
}
.sc-float-right {
float: right;
}
/*****************/
.content {
height: calc(100% - 50px);
position: absolute;
top: 80px;
left: 0px;
width: 100%;
overflow: hidden;
background: #f3f3f3;
padding: 20px;
}
.profileImage {
height: 40px;
width: 40px;
margin-top: -10px !important;
border-radius: 50%;
}
.headerField {
display: inline-block;
vertical-align: middle;
}
<link href="//cdn.c2cmalls.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
@2947721120
Copy link
Author

image
角火力谷歌登陆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment