1

I have the following code for the url, if url have invited_by then show signup popup on the provided page in the link like this http://streamkar.dev/store/?invited_by=1 , Its basically to show the signup popup.

$(document).ready(function(){
    var a = window.location.href.toString();
    var  b = a.split("invited_by=");
    if(typeof b[1] !=="undefined"){
        console.log(b[1]);
        $("#login-modal").modal('show');
        $(".signin").click();
    }
});

What i want is that how can we check user Authentication, That if a user is already logged in then :

Show :

http://streamkar.dev/store

Instead of :

http://streamkar.dev/store/?invited_by=1

Basically The invitation link can be given to someone, If he/she is already logged in then just show the page, If not then show the popup for signup then redirect to the page in the url, Its basically invitation.

How can i achieve that, I am not coming up with some suitable logic.

1
  • Your question is entirely about javascript, but you tagged it with php and laravel, too. And why would you redirect with javascript instead of using the built-in routes in laravel? Commented Jul 17, 2016 at 17:50

2 Answers 2

2

You can try something like below,

In the controller, check user logged AND query has invited_by param, IF so redirect to store ELSE render the view.

public function getIndex(Request $request)
{
    if (Auth::check() && $request->has('invited_by')) {
        return redirect('/store');
    } else {
        return view('path_to_view')
               ->with('userLogged', Auth::check());
    }
}

In the .blade file, render the modal html if user not logged.

@if (! $userLogged)
    <div id="login-modal"></div> //model html
@endif

in javascript, show the modal if dom has div with id login-modal

if(typeof b[1] !=="undefined" && $("#login-modal").length > 0){
    $("#login-modal").modal('show');
    $(".signin").click();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks bud, I have a question,I have alot of views functions, Is there anyway to create one function or anything else which can be used for every view function ?
ah its simple you can use Auth::check() in blade file, or you can use view composers laravel.com/docs/5.1/views#view-composers
0

what you need to do is from Controller function store,check if user already authenticated

if (Auth::check()) {
    return redirect()->to('store')
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.