Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular/Node authenticate User

I have a node server that is running. and it sends a post to a Twitter Timeline, which I have an Ionic/Angular Application the consumes the Node Server and sends the message.

however, the problem is that in the node server my user's Twitter account info is hard coded and I would like to know how I can send the user's details that I get from the Twitter connect Plugin.

here is my node sever

const express = require('express');
const Twitter = require('twit');

const app = express();
const client = new Twitter({
  consumer_key:         '...',
  consumer_secret:      '...',
  access_token:         '...',
  access_token_secret:  '...',
});


app.use(require('cors')());
app.use(require('body-parser').json());

app.post('/post_tweet', (req, res) => {

  tweet = req.body;

  client
    .post(`statuses/update`, tweet)
    .then(tweeting => {
      console.log(tweeting);

      res.send(tweeting);
    })

   .catch(error => {
    res.send(error);
  });


});

app.listen(3000, () => console.log('Server running'));

twitter service in my angular/ionic app

export class TwitterserviceService {
  api_url = 'http://localhost:3000';

  constructor(private http: HttpClient) { } 

  tweet(tweetdata: string) {
    return this.http.post<any>(`${this.api_url}/post_tweet/`, {status: tweetdata})
        .pipe(map(tweet => {

            alert("tweet posted")

            return tweet;
        }));
}
}

and here is my angular Code that sends a tweet to the node server

sendTweet() {  
  this.api.tweet('sent from phone')
            .pipe(first())
            .subscribe(
                data => {
                    console.log('yes')
                },
                error => {
                  'failed'
                });
}

connect plugin

twitterLogin() {
    this.twitter.login().then((res: any) => {
      localStorage.setItem('twitterLogin', "true");
      this.firebaseService.twitterDetail(this.userId, res);


      this.twitterUser = true;
      if(this.twitterUser == true){
        this.twitter.showUser()
        .then(user => {
          console.log("User: " + user +
                        'username ' + res.username+ " " + user.username);

        });
      }


    }, (err: any) => {
      alert('error: ' + err);
    });
  }
like image 749
Mitchell Yuen Avatar asked Nov 20 '19 20:11

Mitchell Yuen


People also ask

How do I authenticate in Angular 8?

Open command prompt and go to project root folder. Start the application. Create a new service, AuthService to authenticate the user. Open AuthService and include below code.

How is Angular authentication done?

Your Angular application authenticates the user and receives an access token from Auth0. The application can then pass that access token to your API as a credential. In turn, your API can use Auth0 libraries to verify the access token it receives from the calling application and issue a response with the desired data.

What is JWT authentication in Angular?

JSON web tokens (JWTs) provide a method of authenticating requests that's convenient, compact, and secure. More often than not, Angular apps will include them in their data flows.


1 Answers

You will need to set params to AuthenticationHeaders while calling the API service which will implement Interceptor interface in Angular.

You can use this for reference HttpRequest with Interceptor in Angular

like image 54
lousybear Avatar answered Oct 10 '22 20:10

lousybear