Thanks! Really.
It is a SaaS app, with Twitter oAuth login and I use stripe for subscription verification.
Login code…all of it (routing towards very bottom):
/**
- Created by Greg on 7/11/2016.
*/
import {Component, OnInit} from ‘@angular/core’;
import { Meteor } from ‘meteor/meteor’;
import { MeteorComponent} from ‘angular2-meteor’;
import { SSMainDataService } from ‘…/main_data.service’;
import { FormBuilder, ControlGroup, Validators } from ‘@angular/common’;
import {Companies} from ‘…/…/collections/Companys’;
import { Router } from ‘@angular/router’;
declare var Cookie:any;
function emailValidator(control) {
var EMAIL_REGEXP = /^[a-z0-9!#$%&’+/=?^_`{|}~.-]+@a-z0-9?(.a-z0-9?)$/i;
if (!EMAIL_REGEXP.test(control.value)) {
return {invalidEmail: true};
}
}
//noinspection TypeScriptCheckImport
import template from ‘./login.component.html’;
@Component({
selector: ‘login’,
template,
styleUrls: [‘login.component.css’]
})
export class LoginComponent extends MeteorComponent implements OnInit {
public errMsg:String = '';
needEmail: ReactiveVar<boolean> = new ReactiveVar<boolean>(false);
loggedIn: ReactiveVar<boolean> = new ReactiveVar<boolean>(false);
public emailForm: ControlGroup;
constructor( private mainDD: SSMainDataService, private router: Router ) {
super();
this.autorun(()=>{
this.needEmail.get();
this.loggedIn.get();
});
}
ngOnInit() {
if (Cookie.get('SSLoginSA')){
console.log("Yes Cookies Set ... Log In...");
this.loggedIn.set(true);// = true;
this.login(Cookie.get('SSLoginSA'));
}
}
twitterLogin(){
Meteor.loginWithTwitter((err) => {
if (err) {
console.log(err);
this.errMsg = err.toString();
} else {
this.loggedIn.set(true);
this.login(Meteor.userId());
}
});
}
login(userId:string){
this.call('is.valid.user', userId, (err, result)=> {
if (err) {
this.loggedIn.set(false);
this.errMsg = "Weird... try again!"
return;
}
if (!result) {
this.loggedIn.set(false);
this.errMsg = "Invalid User... try again!"
return;
}
this.subscribe('myCompany', userId, () => {
this.mainDD.myCompany = Companies.findOne(); // only one
if (this.mainDD.myCompany){
// good
this.mainDD.loginId = userId;
Cookie.set('SSLoginSA', userId);
this.checkEmail(userId);
} else {
// not setup YET
//this.errMsg = "Login in failed, no Company found... try again!";
this.call('add.company.subscription', userId, "basic_v2", (error, company) => {
if (error) {
this.errMsg = "Failed to create company and subscription subscription. Error: " + error;
this.loggedIn.set(false);
} else {
this.mainDD.myCompany = company;
this.mainDD.loginId = userId;
Cookie.set('SSLoginSA', userId);
this.checkEmail(userId);
}
}, true);
}
}, true);
}, true);
}
checkEmail(userId:string) {
console.log('Check to see if the loggedin user has eMail!');
this.subscribe('myProfile', userId, () => {
this.mainDD.myUser = Meteor.users.findOne();
// console.log("myProfile: " + this.mainDD.toString())
if (this.mainDD.myUser["emails"]){
// has email
this.checkSubscription();
} else {
this.needEmail.set(true);// = true;
let fb = new FormBuilder();
this.emailForm = fb.group({
"email": ["", Validators.compose([emailValidator])]
});
}
}, true);
}
addemail(myemail: string) {
if (this.emailForm.valid){
//console.log("myEmail: " + myemail);
this.call("add.user.email", this.mainDD.loginId, myemail); // add it
this.needEmail.set(false);
this.checkSubscription();
}
}
checkSubscription() {
console.log('Check Subscription status!');
this.call('get.subscription.status', this.mainDD.myCompany.stripe_subscription_id, (error, status) => {
if (error) {
this.errMsg = "Failed to validate subscription. Error: "+error;
} else {
console.log('Subscription status: ' + status);
this.errMsg = "Status: " + status + "!";
this.mainDD.loginSuccess = true;
if (status == "trialing") {
this.mainDD.isTrial = true;
}
if (status == "trialing" || status == "active") {
this.router.navigate(['/main']);
} else {
//past_due, canceled, or unpaid
this.mainDD.subscriptionStatus = status;
this.router.navigate(['/subscribe']);
}
}
}, true);
}
clearError(){
this.errMsg = "";
}
}